From db3fbb52b2b5696ca824b690b04ccfcf1bf5c88a Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 30 Nov 2007 20:01:59 -0600 Subject: [PATCH] Add map-until and a unit test for it --- extra/sequences/lib/lib-tests.factor | 2 ++ extra/sequences/lib/lib.factor | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/extra/sequences/lib/lib-tests.factor b/extra/sequences/lib/lib-tests.factor index c170a0d20a..e1257748b3 100644 --- a/extra/sequences/lib/lib-tests.factor +++ b/extra/sequences/lib/lib-tests.factor @@ -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 diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index 33cfe80fcc..0de90b74d6 100644 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -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 ;