math.extras: adding moving-count.

db4
John Benediktsson 2012-08-31 11:04:12 -07:00
parent 20100d7853
commit a454b2e384
2 changed files with 6 additions and 1 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: math.extras math.ranges tools.test ;
USING: math math.extras math.ranges tools.test ;
IN: math.extras.test
@ -21,6 +21,8 @@ IN: math.extras.test
{ { 2 5 5 4 3 } } [ { 1 2 5 6 1 4 3 } 3 moving-median ] unit-test
{ { 2 1 1 2 1 1 } } [ { 1 1 2 3 5 8 13 } 2 [ odd? ] moving-count ] unit-test
{ { } } [ { 0 0 } nonzero ] unit-test
{ { 1 2 3 } } [ { 0 1 0 2 0 3 0 } nonzero ] unit-test

View File

@ -105,6 +105,9 @@ PRIVATE>
: moving-sum ( u n -- v )
<clumps> [ sum ] map ;
: moving-count ( ... u n quot: ( ... elt -- ... ? ) -- ... v )
[ <clumps> ] [ [ count ] curry map ] bi* ; inline
: nonzero ( seq -- seq' )
[ zero? not ] filter ;