mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
arrays, maps: add indexed variant of collection function and minor cleanup (#15948)
This commit is contained in:
@ -24,8 +24,8 @@ pub fn to_array<K, V, I>(m map[K]V, f fn (K, V) I) []I {
|
||||
return a
|
||||
}
|
||||
|
||||
// flatten maps map entries into arrays and flattens into a one-dimensional array
|
||||
pub fn flatten<K, V, I>(m map[K]V, f fn (K, V) []I) []I {
|
||||
// flat_map maps map entries into arrays and flattens into a one-dimensional array
|
||||
pub fn flat_map<K, V, I>(m map[K]V, f fn (K, V) []I) []I {
|
||||
mut a := []I{cap: m.len}
|
||||
|
||||
for k, v in m {
|
||||
|
@ -36,13 +36,13 @@ fn test_to_array() {
|
||||
}) == ['abc', 'def', 'ghi']
|
||||
}
|
||||
|
||||
fn test_flatten() {
|
||||
fn test_flat_map() {
|
||||
m1 := {
|
||||
1: [2, 3]
|
||||
4: [5, 6]
|
||||
7: [8, 9]
|
||||
}
|
||||
assert flatten<int, []int, int>(m1, fn (k int, v []int) []int {
|
||||
assert flat_map<int, []int, int>(m1, fn (k int, v []int) []int {
|
||||
mut a := [k]
|
||||
a << v
|
||||
return a
|
||||
|
Reference in New Issue
Block a user