From 09dfc3f301e7e12cb691de130b3d115edb44a3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= <33205215+remimimimi@users.noreply.github.com> Date: Sun, 26 Sep 2021 16:41:50 +0000 Subject: [PATCH] arrays: add a generic `arrays.concat(os.args,'abc','xyz')` function (#11985) --- vlib/arrays/arrays.v | 10 ++++++++++ vlib/arrays/arrays_test.v | 14 ++++++++++++++ 2 files changed, 24 insertions(+) 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'] +}