feat: add Sequence.each method to iterate over elements with a function
Add a new `each` method to the Sequence class in the Wren programming language, allowing iteration over sequence elements by passing each element to a provided function. This replaces the previous pattern of using `map` with `Fiber.yield` in examples, providing a more direct and idiomatic way to perform side effects per element. The implementation includes a core library method in `core.wren`, a C source string in `wren_core.c`, documentation in the Sequence markdown file, and three test cases covering normal iteration, empty sequences, and non-function argument error handling.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user