expr
expr copied to clipboard
Combine 2 arrays
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"]
Right now Expr doesn't have a built-in concat function.
But it can be easily added via expr.Function
Sure @antonmedv, but I think its commonly enough to add it here as well
What should we add? append() or concat()? :)
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]]
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]
}
Ok append it is :) I still think that concat is the better function name, but could be bias.
I like concat()?
I also think concat() is the better wording for that.
I love JS, so will vote for concat(). And one element can be wrapped in array.
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.
I think with slices.Concat added in Golang 1.22(to merge to slices), the name should be concat
So it will be concat
Added!