diff --git a/vlib/arrays/arrays.v b/vlib/arrays/arrays.v index cc6558e4fc..1dd3310755 100644 --- a/vlib/arrays/arrays.v +++ b/vlib/arrays/arrays.v @@ -280,3 +280,13 @@ pub fn group_by(list []V, grouping_op fn (v V) K) map[K][]V { return result } + +// concatenate two arrays +pub fn concat(a []T, b ...T) []T { + mut m := []T{cap: a.len + b.len} + + m << a + m << b + + return m +} diff --git a/vlib/arrays/arrays_test.v b/vlib/arrays/arrays_test.v index 8beb413393..29cc248b2a 100644 --- a/vlib/arrays/arrays_test.v +++ b/vlib/arrays/arrays_test.v @@ -180,3 +180,17 @@ fn test_group_by() { return 0 }) == map[int][]int{} } + +fn test_concat_int() { + mut a := [1, 2, 3] + mut b := [3, 2, 1] + + assert concat(a, ...b) == [1, 2, 3, 3, 2, 1] +} + +fn test_concat_string() { + mut a := ['1', '2', '3'] + mut b := ['3', '2', '1'] + + assert concat(a, ...b) == ['1', '2', '3', '3', '2', '1'] +}