mirror of
https://github.com/vlang/v.git
synced 2025-09-14 23:12:33 +03:00
checker: support lambda expressions in array methods like a.map(|x|x*10)
too (#19424)
This commit is contained in:
parent
9954f89e3f
commit
a685088fbd
7 changed files with 204 additions and 90 deletions
35
vlib/builtin/lambda_expr_array_test.v
Normal file
35
vlib/builtin/lambda_expr_array_test.v
Normal file
|
@ -0,0 +1,35 @@
|
|||
const a = [4, 5, 1, 2, 5, 9]
|
||||
|
||||
fn test_map() {
|
||||
assert a.map(it) == a
|
||||
assert a.map(it * 10) == [40, 50, 10, 20, 50, 90]
|
||||
//
|
||||
assert a.map(|x| x) == a
|
||||
assert a.map(|x| x * 10) == [40, 50, 10, 20, 50, 90]
|
||||
assert a.map(|x| 'x: ${x}') == ['x: 4', 'x: 5', 'x: 1', 'x: 2', 'x: 5', 'x: 9']
|
||||
assert a.map(|x| f64(x) * 10.0) == [40.0, 50.0, 10.0, 20.0, 50.0, 90.0]
|
||||
}
|
||||
|
||||
fn test_filter() {
|
||||
assert a.filter(it > 4) == [5, 5, 9]
|
||||
assert a.filter(it < 4) == [1, 2]
|
||||
//
|
||||
assert a.filter(|x| x > 4) == [5, 5, 9]
|
||||
assert a.filter(|x| x < 4) == [1, 2]
|
||||
}
|
||||
|
||||
fn test_any() {
|
||||
assert a.any(it > 4)
|
||||
assert !a.any(it > 40)
|
||||
|
||||
assert a.any(|x| x > 4)
|
||||
assert !a.any(|x| x > 40)
|
||||
}
|
||||
|
||||
fn test_all() {
|
||||
assert !a.all(it > 4)
|
||||
assert a.all(it < 40)
|
||||
|
||||
assert !a.all(|x| x > 4)
|
||||
assert a.all(|x| x < 40)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue