Array Concatenation?

Is there a way to concatenate two arrays or lists easily? Like, if I have an array A = [1, 2] and an array B = [3, 4], is there any operation I can perform on A and B to easily get a result of [1, 2, 3, 4]?

Hi Vanessa —

For lists, I think you can use the second append() method overload listed at: List — Chapel Documentation 1.29

For arrays, there's no built-in append/concatenate method. Here's one way to make it happen:

var A = [1, 2];
var B = [3, 4];

var totsize = A.size + B.size;
var C: [0..<totsize] A.eltType;
C[0..<A.size] = A;
C[A.size..] = B;

writeln(C);

Or using 1-based indexing:

var A = [1, 2];
var B = [3, 4];

var totsize = A.size + B.size;
var C: [1..totsize] A.eltType;
C[1..A.size] = A;
C[A.size+1..] = B;
writeln(C);

-Brad

1 Like

Thanks Brad!

Do you see an array concatenation operator being implemented in Chapel in the future?

It's not something we've considered... Array reallocation tends to be expensive (whereas our list implementation is designed to grow over time), which I believe is why we don't support one currently. But if you (or someone else reading) wanted to champion it, filing a feature request issue on our GitHub repo would completely be fair game.

-Brad

Where is the best reference to learn to use Chapel lists. The specification

https://chapel-lang.org/docs/modules/standard/List.html

is what you would use once you know how to exloit a list i CHapel. The examples in the release are a bit hard to use as a learning tool. I cannot find a primer but maybe I am searching poorly.

Thanks ...

Hi Vanessa,

Do you have some more extensive examples of where you might want to append/concatentate arrays. With pre-empting what they might show, I think that Brad's approach is likely to be far more efficient, at least for the types of problems I encounter - which is not necessarily the same as what you encounter.

Damian, how does this primer look to you?

https://chapel-lang.org/docs/primers/listOps.html

Brad, I can imagine an array concatenation operation that produces an iterable, so that there is no storage cost unless the user chooses to store the result in a variable.

Vanessa, the + operator on arrays is an element-wise promoted addition, so we could not use it to indicate concatenation.

1 Like

Thanks Vass. I finally found that primer myself late last night. For some reason, my search skills through the Chapel documentation is poor.

I might be slow, but I think that most of that primer's content should be at the start of the main doco.The primer needs to go into a little more detail. I am still trying to figure out how to use the operators or even what the last few actually do. That said, I almost never use a list in my own work, certainly in the context of my work which is dominated by array work. But Vanessa might work in a different area where they are needed.

Damian —

Our documentation that's generated from module code / chpldoc does not support automated testing today, which is the main reason there aren't many examples in the documentation outside of the spec and primer. This is something we'd like to enable but have not yet, and the following issue captures the desire:

-Brad

Thanks for that information Brad. I figured it was something you had well in hand.

There is an existing issue (8802) requesting array concatenation, though it hasn't had any activity in a few years.