mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Merge branch 'master' of git://github.com/bjorn/wren into bjorn-master
This commit is contained in:
@ -22,6 +22,13 @@ class Sequence {
|
||||
return true
|
||||
}
|
||||
|
||||
any(f) {
|
||||
for (element in this) {
|
||||
if (f.call(element)) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
reduce(acc, f) {
|
||||
for (element in this) {
|
||||
acc = f.call(acc, element)
|
||||
|
||||
@ -11,13 +11,25 @@ core [iterator protocol][] can extend this to get a number of helpful methods.
|
||||
Tests whether all the elements in the sequence pass the `predicate`.
|
||||
|
||||
Iterates over the sequence, passing each element to the function `predicate`.
|
||||
If it returns `false`, stops iterating and returns `false`. Otherwise, returns
|
||||
`true`.
|
||||
If its return value evaluates to `false`, stops iterating and returns `false`.
|
||||
Otherwise, returns `true`.
|
||||
|
||||
:::dart
|
||||
[1, 2, 3].all {|n| n > 2} // False.
|
||||
[1, 2, 3].all {|n| n < 4} // True.
|
||||
|
||||
### **any**(predicate)
|
||||
|
||||
Tests whether any element in the sequence passes the `predicate`.
|
||||
|
||||
Iterates over the sequence, passing each element to the function `predicate`.
|
||||
If its return value evaluates to `true`, stops iterating and returns `true`.
|
||||
Otherwise, returns `false`.
|
||||
|
||||
:::dart
|
||||
[1, 2, 3].any {|n| n < 1} // False.
|
||||
[1, 2, 3].any {|n| n > 2} // True.
|
||||
|
||||
### **join**(sep)
|
||||
|
||||
Returns a string representation of the list. The string representations of the
|
||||
|
||||
@ -61,12 +61,12 @@ Here's the same example in Wren:
|
||||
}
|
||||
|
||||
// create and use an Account
|
||||
var acc = new Account(100)
|
||||
var acc = new Account(1000)
|
||||
acc.withdraw(100)
|
||||
|
||||
Classes have a reputation for complexity because most of the widely used
|
||||
languages with them are quite complex: C++, Java, C#, Ruby, and Python. I hope
|
||||
to show with Wren that is those languages that are complex, and not classes
|
||||
to show with Wren that it is those languages that are complex, and not classes
|
||||
themselves.
|
||||
|
||||
Smalltalk, the language that inspired most of those languages, is famously
|
||||
|
||||
@ -68,6 +68,13 @@ static const char* libSource =
|
||||
" return true\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" any(f) {\n"
|
||||
" for (element in this) {\n"
|
||||
" if (f.call(element)) return true\n"
|
||||
" }\n"
|
||||
" return false\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" reduce(acc, f) {\n"
|
||||
" for (element in this) {\n"
|
||||
" acc = f.call(acc, element)\n"
|
||||
|
||||
10
test/list/any.wren
Normal file
10
test/list/any.wren
Normal file
@ -0,0 +1,10 @@
|
||||
var a = [1, 2, 3]
|
||||
|
||||
var b = a.any {|x| x > 3 }
|
||||
IO.print(b) // expect: false
|
||||
|
||||
var d = a.any {|x| x > 1 }
|
||||
IO.print(d) // expect: true
|
||||
|
||||
var e = [].any {|x| true }
|
||||
IO.print(e) // expect: false
|
||||
1
test/list/any_non_bool_returning_fn.wren
Normal file
1
test/list/any_non_bool_returning_fn.wren
Normal file
@ -0,0 +1 @@
|
||||
IO.print([1, 2, 3].any {|x| "truthy" }) // expect: true
|
||||
1
test/list/any_non_function_arg.wren
Normal file
1
test/list/any_non_function_arg.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].any("string") // expect runtime error: String does not implement 'call(_)'.
|
||||
Reference in New Issue
Block a user