Merge branch 'master' of git://github.com/bjorn/wren into bjorn-master

This commit is contained in:
Bob Nystrom
2015-03-03 07:23:47 -08:00
7 changed files with 42 additions and 4 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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
View 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

View File

@ -0,0 +1 @@
IO.print([1, 2, 3].any {|x| "truthy" }) // expect: true

View File

@ -0,0 +1 @@
[1, 2, 3].any("string") // expect runtime error: String does not implement 'call(_)'.