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:
@@ -0,0 +1,3 @@
|
||||
var words = ""
|
||||
["One", "Two", "Three"].each {|word| words = words + word }
|
||||
IO.print(words) // expect: OneTwoThree
|
||||
@@ -0,0 +1,3 @@
|
||||
var i = 0
|
||||
[].each {|item| i = i + 1 }
|
||||
IO.print(i) // expect: 0
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3].each("string") // expect runtime error: String does not implement 'call(_)'.
|
||||
Reference in New Issue
Block a user