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:
Bob Nystrom
2015-04-01 14:10:21 +00:00
8 changed files with 29 additions and 3 deletions
+3
View File
@@ -0,0 +1,3 @@
var words = ""
["One", "Two", "Three"].each {|word| words = words + word }
IO.print(words) // expect: OneTwoThree
+3
View File
@@ -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(_)'.