expr icon indicating copy to clipboard operation
expr copied to clipboard

Combine 2 arrays

Open lukas-vollmer opened this issue 1 year ago • 9 comments

Is there currently a way to combine arrays?

Something like this: concat(["value1", "value2"], ["value3", "value4"]) or ["value1", "value2"] + ["value3", "value4"]

Result:

["value1", "value2", "value3", "value4"]

lukas-vollmer avatar Dec 06 '23 11:12 lukas-vollmer

Right now Expr doesn't have a built-in concat function.

But it can be easily added via expr.Function

antonmedv avatar Dec 08 '23 18:12 antonmedv

Sure @antonmedv, but I think its commonly enough to add it here as well

lukas-vollmer avatar Jan 04 '24 14:01 lukas-vollmer

What should we add? append() or concat()? :)

antonmedv avatar Jan 04 '24 14:01 antonmedv

I think concat() would make more sense here, since append would only add the array as a new entry: concat:

array1 = [1, 2, 3]
array2 = [4, 5, 6]
array3 = concat(array1, array2) -> [1, 2, 3, 4, 5, 6]

Append would be:

array1 = [1, 2, 3]
array2 = [4, 5, 6]
array3 = append(array1, array2) -> [1, 2, 3, [4, 5, 6]]

lukas-vollmer avatar Jan 04 '24 15:01 lukas-vollmer

Hmm just checked it for go. It seems different here xD

package main

import (
    "fmt"
)

func main() {
    slice1 := []int{1, 2, 3}
    slice2 := []int{4, 5, 6}

    // Concatenating slice1 and slice2
    concatenatedSlice := append(slice1, slice2...)

    fmt.Println(concatenatedSlice) // Output: [1 2 3 4 5 6]
}

lukas-vollmer avatar Jan 04 '24 15:01 lukas-vollmer

Ok append it is :) I still think that concat is the better function name, but could be bias.

lukas-vollmer avatar Jan 04 '24 15:01 lukas-vollmer

I like concat()?

antonmedv avatar Jan 04 '24 16:01 antonmedv

I also think concat() is the better wording for that.

lukas-vollmer avatar Jan 04 '24 17:01 lukas-vollmer

I love JS, so will vote for concat(). And one element can be wrapped in array.

annamedv avatar Jan 05 '24 12:01 annamedv

Not sure if this deserves its own issue but I found myself wanting a way to merge maps as well. I've used Python's dictionary unions like this before.

elliottc avatar Jan 24 '24 19:01 elliottc

I think with slices.Concat added in Golang 1.22(to merge to slices), the name should be concat

kuadrosx avatar Jan 30 '24 00:01 kuadrosx

So it will be concat

antonmedv avatar Feb 12 '24 11:02 antonmedv

Added!

antonmedv avatar Feb 17 '24 23:02 antonmedv