math.ranges: implement sum on ranges.

db4
John Benediktsson 2013-05-06 16:01:29 -07:00
parent bca3ddfae8
commit fe45a08054
2 changed files with 8 additions and 1 deletions

View File

@ -1,4 +1,4 @@
USING: math math.ranges sequences sets tools.test arrays ;
USING: arrays kernel math math.ranges sequences sets tools.test ;
IN: math.ranges.tests
[ { } ] [ 1 1 (a,b) >array ] unit-test
@ -51,3 +51,8 @@ IN: math.ranges.tests
[ 100 ] [
1 100 [a,b] [ 2^ [1,b] ] map members length
] unit-test
{ t } [ -10 10 1 <range> [ sum ] [ >array sum ] bi = ] unit-test
{ t } [ -10 10 2 <range> [ sum ] [ >array sum ] bi = ] unit-test
{ t } [ 10 -10 -1 <range> [ sum ] [ >array sum ] bi = ] unit-test
{ t } [ 10 -10 -2 <range> [ sum ] [ >array sum ] bi = ] unit-test

View File

@ -32,6 +32,8 @@ M: range hashcode* tuple-hashcode ;
INSTANCE: range immutable-sequence
M: range sum [ length ] [ first ] [ last ] tri + * 2 / ;
<PRIVATE
: twiddle ( a b -- a b step ) 2dup > -1 1 ? ; inline