diff --git a/extra/combinators/lib/lib-tests.factor b/extra/combinators/lib/lib-tests.factor index 43385b911d..0d76e6f50d 100644 --- a/extra/combinators/lib/lib-tests.factor +++ b/extra/combinators/lib/lib-tests.factor @@ -58,3 +58,5 @@ IN: temporary [ dup array? ] [ dup vector? ] [ dup float? ] } || nip ] unit-test + +[ 1 2 3 4 ] [ { 1 2 3 4 } 4 nfirst ] unit-test diff --git a/extra/combinators/lib/lib.factor b/extra/combinators/lib/lib.factor index 3f49da7cb3..fe11fd1338 100644 --- a/extra/combinators/lib/lib.factor +++ b/extra/combinators/lib/lib.factor @@ -67,6 +67,12 @@ MACRO: napply ( n -- ) : map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline +MACRO: nfirst ( n -- ) + [ [ swap nth ] curry [ keep ] curry ] map concat [ drop ] compose ; + +: seq>stack ( seq -- ) + dup length nfirst ; inline + ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : sigma ( seq quot -- n ) [ rot slip + ] curry 0 swap reduce ;