feat: add reduce method to Sequence with two overloads and tests

Implement `reduce` on the `Sequence` class in core.wren with two overloads: one taking an initial accumulator and a function, and another using the first element as the initial accumulator. Add three test files covering normal reduction, single-element edge case, and wrong arity error.
This commit is contained in:
Luchs
2015-01-15 15:29:49 +00:00
parent d3ea153a31
commit afa0a0a8e9
5 changed files with 31 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
var a = [1, 4, 2, 1, 5]
var max = new Fn {|a, b| a > b ? a : b }
var sum = new Fn {|a, b| a + b }
IO.print(a.reduce(max)) // expect: 5
IO.print(a.reduce(10, max)) // expect: 10
IO.print(a.reduce(sum)) // expect: 13
IO.print(a.reduce(-1, sum)) // expect: 12
+1
View File
@@ -0,0 +1 @@
[1].reduce {|a, b| a } // expect runtime error: Range start out of bounds.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].reduce {|x, y, z| x } // expect runtime error: Function expects more arguments.