mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Merge branch 'list-forall' of git://github.com/gsmaverick/wren into gsmaverick-list-forall
This commit is contained in:
@ -14,6 +14,13 @@ class Sequence {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
all(f) {
|
||||
for (element in this) {
|
||||
if (!f.call(element)) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
class List is Sequence {
|
||||
|
||||
@ -313,6 +313,10 @@ Removes all items from the list.
|
||||
|
||||
The number of items in the list.
|
||||
|
||||
### **forall(predicate)**
|
||||
|
||||
Tests whether all the elements in the list pass the `predicate`.
|
||||
|
||||
### **insert**(item, index)
|
||||
|
||||
**TODO**
|
||||
|
||||
@ -57,6 +57,13 @@ static const char* libSource =
|
||||
" }\n"
|
||||
" return result\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" all(f) {\n"
|
||||
" for (element in this) {\n"
|
||||
" if (!f.call(element)) return false\n"
|
||||
" }\n"
|
||||
" return true\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"class List is Sequence {\n"
|
||||
|
||||
9
test/list/forall.wren
Normal file
9
test/list/forall.wren
Normal file
@ -0,0 +1,9 @@
|
||||
var a = [1, 2, 3]
|
||||
var b = a.all {|x| x > 1 }
|
||||
IO.print(b) // expect: false
|
||||
|
||||
var d = a.all {|x| x > 0 }
|
||||
IO.print(d) // expect: true
|
||||
|
||||
var e = [].all {|x| false }
|
||||
IO.print(e) // expect: true
|
||||
1
test/list/forall_non_bool_returning_fn.wren
Normal file
1
test/list/forall_non_bool_returning_fn.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].all {|x| "string" } // expect runtime error: String does not implement method '!' with 0 arguments.
|
||||
1
test/list/forall_non_function_arg.wren
Normal file
1
test/list/forall_non_function_arg.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].all("string") // expect runtime error: String does not implement method 'call' with 1 argument.
|
||||
Reference in New Issue
Block a user