Cleaning up strict list combinators

db4
Daniel Ehrenberg 2009-02-09 15:31:57 -06:00
parent 8aa729abb1
commit 462b208475
3 changed files with 30 additions and 24 deletions

View File

@ -1,7 +1,6 @@
! Copyright (C) 2008 James Cash ! Copyright (C) 2008 James Cash
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: tools.test lists math ; USING: tools.test lists math kernel ;
IN: lists.tests IN: lists.tests
{ { 3 4 5 6 7 } } [ { { 3 4 5 6 7 } } [
@ -68,3 +67,5 @@ IN: lists.tests
{ { 1 2 3 4 5 6 } } [ { { 1 2 3 4 5 6 } } [
{ 1 2 3 } sequence>cons { 4 5 6 } sequence>cons lappend list>array { 1 2 3 } sequence>cons { 4 5 6 } sequence>cons lappend list>array
] unit-test ] unit-test
[ { 1 } { 2 } ] [ { 1 2 } sequence>cons 1 lcut [ list>array ] bi@ ] unit-test

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 James Cash ! Copyright (C) 2008 James Cash
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences accessors math arrays vectors classes words USING: kernel sequences accessors math arrays vectors classes words
combinators.short-circuit combinators ; combinators.short-circuit combinators locals ;
IN: lists IN: lists
! List Protocol ! List Protocol
@ -25,7 +25,7 @@ M: +nil+ nil? drop t ;
M: object nil? drop f ; M: object nil? drop f ;
: atom? ( obj -- ? ) : atom? ( obj -- ? )
{ [ list? ] [ nil? ] } 1|| not ; list? not ;
: nil ( -- symbol ) +nil+ ; : nil ( -- symbol ) +nil+ ;
@ -76,10 +76,10 @@ PRIVATE>
: foldl ( list identity quot: ( obj1 obj2 -- obj ) -- result ) : foldl ( list identity quot: ( obj1 obj2 -- obj ) -- result )
swapd leach ; inline swapd leach ; inline
: foldr ( list identity quot: ( obj1 obj2 -- obj ) -- result ) :: foldr ( list identity quot: ( obj1 obj2 -- obj ) -- result )
pick nil? [ [ drop ] [ ] [ drop ] tri* ] [ list nil? [ identity ] [
[ [ cdr ] 2dip foldr ] [ nip [ car ] dip ] 3bi list cdr identity quot foldr
call list car quot call
] if ; inline recursive ] if ; inline recursive
: llength ( list -- n ) : llength ( list -- n )
@ -92,7 +92,7 @@ PRIVATE>
[ lreverse ] dip [ swap cons ] foldl ; [ lreverse ] dip [ swap cons ] foldl ;
: lcut ( list index -- before after ) : lcut ( list index -- before after )
[ +nil+ ] dip [ nil ] dip
[ [ [ cdr ] [ car ] bi ] dip cons ] times [ [ [ cdr ] [ car ] bi ] dip cons ] times
lreverse swap ; lreverse swap ;
@ -109,23 +109,27 @@ PRIVATE>
[ tuck same? [ deep-sequence>cons ] when swons ] with reduce ; [ tuck same? [ deep-sequence>cons ] when swons ] with reduce ;
<PRIVATE <PRIVATE
: (lmap>array) ( acc cons quot: ( elt -- elt' ) -- newcons ) :: (lmap>vector) ( acc list quot: ( elt -- elt' ) -- acc )
over nil? [ 2drop ] list nil? [ acc ] [
[ [ unswons ] dip [ call ] keep swapd [ suffix ] 2dip (lmap>array) ] if ; list car quot call acc push
inline recursive acc list cdr quot (lmap>vector)
] if ; inline recursive
: lmap>vector ( list quot -- array )
[ V{ } clone ] 2dip (lmap>vector) ; inline
PRIVATE> PRIVATE>
: lmap>array ( list quot -- array )
[ { } ] 2dip (lmap>array) ; inline
: lmap-as ( list quot exemplar -- sequence ) : lmap-as ( list quot exemplar -- sequence )
[ lmap>array ] dip like ; [ lmap>vector ] dip like ; inline
: lmap>array ( list quot -- array )
{ } lmap-as ; inline
: deep-list>array ( list -- array ) : deep-list>array ( list -- array )
[ [
{ {
{ [ dup list? ] [ deep-list>array ] }
{ [ dup nil? ] [ drop { } ] } { [ dup nil? ] [ drop { } ] }
{ [ dup list? ] [ deep-list>array ] }
[ ] [ ]
} cond } cond
] lmap>array ; ] lmap>array ;
@ -133,10 +137,11 @@ PRIVATE>
: list>array ( list -- array ) : list>array ( list -- array )
[ ] lmap>array ; [ ] lmap>array ;
: traverse ( list pred quot: ( list/elt -- result ) -- result ) :: traverse ( list pred quot: ( list/elt -- result ) -- result )
[ list [| elt |
2over call [ tuck [ call ] 2dip ] when elt dup pred call [ quot call ] when
pick list? [ traverse ] [ 2drop ] if dup list? [ pred quot traverse ] when
] 2curry lmap ; inline recursive ] lmap ; inline recursive
INSTANCE: cons list INSTANCE: cons list
INSTANCE: +nil+ list

View File

@ -1,6 +1,6 @@
! Copyright (C) 2009 Daniel Ehrenberg ! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: sequences kernel splitting.monotonic accessors wrap grouping ; USING: sequences kernel splitting.monotonic accessors grouping wrap ;
IN: wrap.words IN: wrap.words
TUPLE: word key width break? ; TUPLE: word key width break? ;