mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-10 21:58:48 +01:00
Added Sequence.each
This is a bit of a style preference since of course you can always write the same thing with a for loop. However, I think sometimes the code looks better when using this method. It also provides an alternative to Sequence.map for cases where you don't need the resulting list, and one that becomes especially necessary when Sequence.map is changed to return a new sequence. The example in the README.md file was using Sequence.map in a way that required this alternative in that case.
This commit is contained in:
@ -13,7 +13,7 @@ class Wren {
|
||||
}
|
||||
|
||||
var adjectives = new Fiber {
|
||||
["small", "clean", "fast"].map {|word| Fiber.yield(word) }
|
||||
["small", "clean", "fast"].each {|word| Fiber.yield(word) }
|
||||
}
|
||||
|
||||
while (!adjectives.isDone) IO.print(adjectives.call())
|
||||
|
||||
@ -26,6 +26,12 @@ class Sequence {
|
||||
return result
|
||||
}
|
||||
|
||||
each(f) {
|
||||
for (element in this) {
|
||||
f.call(element)
|
||||
}
|
||||
}
|
||||
|
||||
map(f) {
|
||||
var result = new List
|
||||
for (element in this) {
|
||||
|
||||
@ -54,6 +54,13 @@ and counting the number of times the returned value evaluates to `true`.
|
||||
[1, 2, 3].count {|n| n > 2} // 1.
|
||||
[1, 2, 3].count {|n| n < 4} // 3.
|
||||
|
||||
### **each**(function)
|
||||
|
||||
Iterates over the sequence, passing each element to the given `function`.
|
||||
|
||||
:::dart
|
||||
["one", "two", "three"].each {|word| IO.print(word) }
|
||||
|
||||
### **join**(sep)
|
||||
|
||||
Returns a string representation of the list. The string representations of the
|
||||
|
||||
@ -15,7 +15,7 @@ a familiar, modern [syntax][].
|
||||
}
|
||||
|
||||
var adjectives = new Fiber {
|
||||
["small", "clean", "fast"].map {|word| Fiber.yield(word) }
|
||||
["small", "clean", "fast"].each {|word| Fiber.yield(word) }
|
||||
}
|
||||
|
||||
while (!adjectives.isDone) IO.print(adjectives.call())
|
||||
@ -52,4 +52,4 @@ If you like the sound of this, [give it a try][try]! Even better, you can
|
||||
[fibers]: fibers.html
|
||||
[embedding]: embedding-api.html
|
||||
[try]: getting-started.html
|
||||
[contribute]: contributing.html
|
||||
[contribute]: contributing.html
|
||||
|
||||
@ -72,6 +72,12 @@ static const char* libSource =
|
||||
" return result\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" each(f) {\n"
|
||||
" for (element in this) {\n"
|
||||
" f.call(element)\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" map(f) {\n"
|
||||
" var result = new List\n"
|
||||
" for (element in this) {\n"
|
||||
|
||||
3
test/core/list/each.wren
Normal file
3
test/core/list/each.wren
Normal file
@ -0,0 +1,3 @@
|
||||
var words = ""
|
||||
["One", "Two", "Three"].each {|word| words = words + word }
|
||||
IO.print(words) // expect: OneTwoThree
|
||||
3
test/core/list/each_no_items.wren
Normal file
3
test/core/list/each_no_items.wren
Normal file
@ -0,0 +1,3 @@
|
||||
var i = 0
|
||||
[].each {|item| i = i + 1 }
|
||||
IO.print(i) // expect: 0
|
||||
1
test/core/list/each_non_function_arg.wren
Normal file
1
test/core/list/each_non_function_arg.wren
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 3].each("string") // expect runtime error: String does not implement 'call(_)'.
|
||||
Reference in New Issue
Block a user