1 ! Copyright (C) 2008 James Cash
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences accessors math arrays vectors classes words locals ;
9 GENERIC: car ( cons -- car )
10 GENERIC: cdr ( cons -- cdr )
11 GENERIC: nil? ( object -- ? )
17 M: cons car ( cons -- car )
20 M: cons cdr ( cons -- cdr )
24 M: word nil? +nil+ eq? ;
25 M: object nil? drop f ;
27 : atom? ( obj -- ? ) [ list? ] [ nil? ] bi or not ;
29 : nil ( -- symbol ) +nil+ ;
31 : uncons ( cons -- cdr car )
34 : 1list ( obj -- cons )
37 : 2list ( a b -- cons )
40 : 3list ( a b c -- cons )
43 : cadr ( cons -- elt )
46 : 2car ( cons -- car caar )
47 [ car ] [ cdr car ] bi ;
49 : 3car ( cons -- car caar caaar )
50 [ car ] [ cdr car ] [ cdr cdr car ] tri ;
52 : lnth ( n list -- elt )
53 swap [ cdr ] times car ;
55 : (leach) ( list quot -- cdr quot )
56 [ [ car ] dip call ] [ [ cdr ] dip ] 2bi ; inline
58 : leach ( list quot: ( elt -- ) -- )
59 over nil? [ 2drop ] [ (leach) leach ] if ; inline recursive
61 : lmap ( list quot: ( elt -- ) -- result )
62 over nil? [ drop ] [ (leach) lmap cons ] if ; inline recursive
64 : foldl ( list identity quot: ( obj1 obj2 -- obj ) -- result )
67 : foldr ( list identity quot: ( obj1 obj2 -- obj ) -- result )
68 pick nil? [ [ drop ] [ ] [ drop ] tri* ] [
69 [ [ cdr ] 2dip foldr ] [ nip [ car ] dip ] 3bi
71 ] if ; inline recursive
73 : llength ( list -- n )
76 : lreverse ( list -- newlist )
77 nil [ swap cons ] foldl ;
79 : lappend ( list1 list2 -- newlist )
80 [ lreverse ] dip [ swap cons ] foldl ;
82 : seq>list ( seq -- list )
83 <reversed> nil [ swap cons ] reduce ;
85 : same? ( obj1 obj2 -- ? )
88 : seq>cons ( seq -- cons )
89 [ <reversed> ] keep nil [ tuck same? [ seq>cons ] when f cons swap >>cdr ] with reduce ;
91 : (lmap>array) ( acc cons quot: ( elt -- elt' ) -- newcons )
93 [ [ uncons ] dip [ call ] keep swapd [ suffix ] 2dip (lmap>array) ] if ;
96 : lmap>array ( cons quot -- newcons )
97 { } -rot (lmap>array) ; inline
99 : lmap-as ( cons quot exemplar -- seq )
100 [ lmap>array ] dip like ;
102 : cons>seq ( cons -- array )
103 [ dup cons? [ cons>seq ] when dup nil? [ drop { } ] when ] lmap>array ;
105 : list>seq ( list -- array )
108 : traverse ( list pred quot: ( list/elt -- result ) -- result )
109 [ 2over call [ tuck [ call ] 2dip ] when
110 pick list? [ traverse ] [ 2drop ] if ] 2curry lmap ; inline recursive