Add map-until and a unit test for it

release
Doug Coleman 2007-11-30 20:01:59 -06:00
parent 8fadc570fc
commit db3fbb52b2
2 changed files with 10 additions and 0 deletions

View File

@ -39,3 +39,5 @@ math.functions tools.test ;
[ 2 ] [ V{ 10 20 30 } [ delete-random drop ] keep length ] unit-test
[ V{ } [ delete-random drop ] keep length ] unit-test-fails
[ { 1 9 25 } ] [ { 1 3 5 6 } [ sq ] [ even? ] map-until ] unit-test

View File

@ -62,3 +62,11 @@ IN: sequences.lib
: delete-random ( seq -- value )
[ length random ] keep [ nth ] 2keep delete-nth ;
: (map-until) ( quot pred -- )
[ dup ] swap 3compose
[ [ drop t ] [ , f ] if ] compose [ find 2drop ] curry ;
: map-until ( seq quot pred -- )
#! Example: { 1 3 5 6 } [ sq ] [ even? ] map-until . -> { 1 9 25 }
(map-until) { } make ;