Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Enumerable#sum

  • Loading branch information...
commit 165ade05bce54881ec6408837dad9aed74720a7e 1 parent 00f4e89
Ola Bini olabini authored
Showing with 24 additions and 0 deletions.
  1. +4 −0 src/builtin/F30_enumerable.ik
  2. +20 −0 test/enumerable_spec.ik
4 src/builtin/F30_enumerable.ik
View
@@ -426,6 +426,10 @@ Mixins Enumerable join = method(
if(index < count - 1, result += separator))
result)
+Mixins Enumerable sum = method(
+ "returns an object created by summing all objects in the enumerable using the + operator. the default value for an empty enumerable will be nil.",
+ inject(+))
+
Mixins Enumerable aliasMethod("map", "collect")
Mixins Enumerable aliasMethod("map", "collect:list")
Mixins Enumerable aliasMethod("map", "map:list")
20 test/enumerable_spec.ik
View
@@ -1022,5 +1022,25 @@ describe(Mixins,
)
)
)
+
+ describe("sum",
+ it("should return nil for an empty enumerable",
+ [] sum should be nil
+ (1...1) sum should be nil
+ #{} sum should be nil
+ )
+
+ it("should return the object in question for a one-object enumerable",
+ [42] sum should == 42
+ (1..1) sum should == 1
+ #{5} sum should == 5
+ ["str"] sum should == "str"
+ )
+
+ it("should use the + operator to sum things",
+ [32, 5, 111, 464] sum should == (32+5+111+464)
+ ["foo", "bar", "bax"] sum should == "foobarbax"
+ )
+ )
)
)
Please sign in to comment.
Something went wrong with that request. Please try again.