]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: implement sum on iota-tuple.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 6 May 2013 23:02:49 +0000 (16:02 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 6 May 2013 23:02:49 +0000 (16:02 -0700)
core/sequences/sequences-tests.factor
core/sequences/sequences.factor

index cf41eb92222afd4281771514d835fec5b83dac03..fd6309da00364f162ce3d5088373ab5be1ee1a5b 100644 (file)
@@ -283,7 +283,7 @@ unit-test
 [ { "d" "a" "b" "c" } ] [ { 3 0 1 2 } { "a" "b" "c" "d" } nths ] unit-test
 
 [ "dac" ] [ { 3 0 2 } "abcd" nths ] unit-test
-                          
+
 TUPLE: bogus-hashcode ;
 
 M: bogus-hashcode hashcode* 2drop 0 >bignum ;
@@ -316,6 +316,10 @@ M: bogus-hashcode hashcode* 2drop 0 >bignum ;
 [ -1 iota ] must-fail
 [ T{ iota-tuple { n 10 } } ] [ 10 iota ] unit-test
 [ 0 ] [ 10 iota first ] unit-test
+{ 0 } [ 0 iota sum ] unit-test
+{ 1 } [ 1 iota sum ] unit-test
+{ 10 } [ 5 iota sum ] unit-test
+{ 15 } [ 6 iota sum ] unit-test
 
 [ "hi" 3 ] [
     { 1 2 3 4 5 6 7 8 } [ H{ { 3 "hi" } } at ] map-find
index de30940973a7db6745cbda2eeda42b0cd836bd8a..caaed80efaa50f65c2809b5b8524f33207419471 100644 (file)
@@ -1006,6 +1006,7 @@ PRIVATE>
 
 GENERIC: sum ( seq -- n )
 M: object sum 0 [ + ] binary-reduce ; inline
+M: iota-tuple sum length dup 1 - * 2/ ; inline
 
 : product ( seq -- n ) 1 [ * ] binary-reduce ;