arrays: add a generic `arrays.concat(os.args,'abc','xyz')` function (#11985)

pull/11987/head
Rémi 2021-09-26 16:41:50 +00:00 committed by GitHub
parent 4c01627e00
commit 09dfc3f301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -280,3 +280,13 @@ pub fn group_by<K, V>(list []V, grouping_op fn (v V) K) map[K][]V {
return result
}
// concatenate two arrays
pub fn concat<T>(a []T, b ...T) []T {
mut m := []T{cap: a.len + b.len}
m << a
m << b
return m
}

View File

@ -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']
}