diff --git a/README.md b/README.md index 75e49f0c..e35551e3 100644 --- a/README.md +++ b/README.md @@ -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()) diff --git a/builtin/core.wren b/builtin/core.wren index a3722dd9..708cc4f4 100644 --- a/builtin/core.wren +++ b/builtin/core.wren @@ -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) { diff --git a/doc/site/core/sequence.markdown b/doc/site/core/sequence.markdown index 807e6c84..62edb140 100644 --- a/doc/site/core/sequence.markdown +++ b/doc/site/core/sequence.markdown @@ -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 diff --git a/doc/site/index.markdown b/doc/site/index.markdown index f6f1ef96..5cecebc5 100644 --- a/doc/site/index.markdown +++ b/doc/site/index.markdown @@ -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 \ No newline at end of file +[contribute]: contributing.html diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index ea75e896..9878be4f 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -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" diff --git a/test/core/list/each.wren b/test/core/list/each.wren new file mode 100644 index 00000000..6629a498 --- /dev/null +++ b/test/core/list/each.wren @@ -0,0 +1,3 @@ +var words = "" +["One", "Two", "Three"].each {|word| words = words + word } +IO.print(words) // expect: OneTwoThree diff --git a/test/core/list/each_no_items.wren b/test/core/list/each_no_items.wren new file mode 100644 index 00000000..cc3754a9 --- /dev/null +++ b/test/core/list/each_no_items.wren @@ -0,0 +1,3 @@ +var i = 0 +[].each {|item| i = i + 1 } +IO.print(i) // expect: 0 diff --git a/test/core/list/each_non_function_arg.wren b/test/core/list/each_non_function_arg.wren new file mode 100644 index 00000000..101554f0 --- /dev/null +++ b/test/core/list/each_non_function_arg.wren @@ -0,0 +1 @@ +[1, 2, 3].each("string") // expect runtime error: String does not implement 'call(_)'.