mirror of
https://github.com/vlang/v.git
synced 2025-09-14 06:52:36 +03:00
arrays: add a partition function, that splits a given array, based on a criteria, passed as a callback fn (#19417)
This commit is contained in:
parent
5905f63e95
commit
04d28f2a74
2 changed files with 32 additions and 0 deletions
|
@ -481,3 +481,20 @@ fn test_join_to_string() {
|
|||
return '1'
|
||||
}) == ''
|
||||
}
|
||||
|
||||
fn test_partition() {
|
||||
a := [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
lower, upper := partition(a, fn (it int) bool {
|
||||
return it < 5
|
||||
})
|
||||
assert lower.len == 4
|
||||
assert upper.len == 4
|
||||
assert lower == [1, 2, 3, 4]
|
||||
assert upper == [5, 6, 7, 8]
|
||||
|
||||
lower2, upper2 := partition(a, fn (it int) bool {
|
||||
return it < 1
|
||||
})
|
||||
assert lower2.len == 0
|
||||
assert upper2.len == 8
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue