]> gitweb.factorcode.org Git - factor.git/commitdiff
Cleaning up strict list combinators
authorDaniel Ehrenberg <littledan@Macintosh-103.local>
Mon, 9 Feb 2009 21:31:57 +0000 (15:31 -0600)
committerDaniel Ehrenberg <littledan@Macintosh-103.local>
Mon, 9 Feb 2009 21:31:57 +0000 (15:31 -0600)
basis/lists/lists-tests.factor
basis/lists/lists.factor
basis/wrap/words/words.factor

index 404a7765059664a1d1b8fa4e04389b70a857ce56..13d2e03e0f1f816ac7eb865ce2fade09b5a974a6 100644 (file)
@@ -1,7 +1,6 @@
 ! Copyright (C) 2008 James Cash
 ! See http://factorcode.org/license.txt for BSD license.
-USING: tools.test lists math ;
-
+USING: tools.test lists math kernel ;
 IN: lists.tests
 
 { { 3 4 5 6 7 } } [
@@ -68,3 +67,5 @@ IN: lists.tests
 { { 1 2 3 4 5 6 } } [
     { 1 2 3 } sequence>cons { 4 5 6 } sequence>cons lappend list>array
 ] unit-test
+
+[ { 1 } { 2 } ] [ { 1 2 } sequence>cons 1 lcut [ list>array ] bi@ ] unit-test
index 784bc95bfe38ee8e981cec93f3304bc308788687..4b0abb7f2d6d249b634c6d5702b60903ebe5f235 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2008 James Cash
 ! See http://factorcode.org/license.txt for BSD license.
 USING: kernel sequences accessors math arrays vectors classes words
-combinators.short-circuit combinators ;
+combinators.short-circuit combinators locals ;
 IN: lists
 
 ! List Protocol
@@ -25,7 +25,7 @@ M: +nil+ nil? drop t ;
 M: object nil? drop f ;
 
 : atom? ( obj -- ? )
-    { [ list? ] [ nil? ] } 1|| not ;
+    list? not ;
 
 : nil ( -- symbol ) +nil+ ; 
 
@@ -76,10 +76,10 @@ PRIVATE>
 : foldl ( list identity quot: ( obj1 obj2 -- obj ) -- result )
     swapd leach ; inline
 
-: foldr ( list identity quot: ( obj1 obj2 -- obj ) -- result )
-    pick nil? [ [ drop ] [ ] [ drop ] tri* ] [
-        [ [ cdr ] 2dip foldr ] [ nip [ car ] dip ] 3bi
-        call
+:: foldr ( list identity quot: ( obj1 obj2 -- obj ) -- result )
+    list nil? [ identity ] [
+        list cdr identity quot foldr
+        list car quot call
     ] if ; inline recursive
 
 : llength ( list -- n )
@@ -92,7 +92,7 @@ PRIVATE>
     [ lreverse ] dip [ swap cons ] foldl ;
 
 : lcut ( list index -- before after )
-    [ +nil+ ] dip
+    [ nil ] dip
     [ [ [ cdr ] [ car ] bi ] dip cons ] times
     lreverse swap ;
 
@@ -109,23 +109,27 @@ PRIVATE>
     [ tuck same? [ deep-sequence>cons ] when swons ] with reduce ;
 
 <PRIVATE
-: (lmap>array) ( acc cons quot: ( elt -- elt' ) -- newcons )
-    over nil? [ 2drop ]
-    [ [ unswons ] dip [ call ] keep swapd [ suffix ] 2dip (lmap>array) ] if ;
-    inline recursive
-PRIVATE>
+:: (lmap>vector) ( acc list quot: ( elt -- elt' ) -- acc )
+    list nil? [ acc ] [
+        list car quot call acc push
+        acc list cdr quot (lmap>vector)
+    ] if ; inline recursive
 
-: lmap>array ( list quot -- array )
-    [ { } ] 2dip (lmap>array) ; inline
+: lmap>vector ( list quot -- array )
+    [ V{ } clone ] 2dip (lmap>vector) ; inline
+PRIVATE>
 
 : 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 )    
     [
         {
-            { [ dup list? ] [ deep-list>array ] }
             { [ dup nil? ] [ drop { } ] }
+            { [ dup list? ] [ deep-list>array ] }
             [ ]
         } cond
     ] lmap>array ;
@@ -133,10 +137,11 @@ PRIVATE>
 : list>array ( list -- array )    
     [ ] lmap>array ;
 
-: traverse ( list pred quot: ( list/elt -- result ) -- result )
-    [
-        2over call [ tuck [ call ] 2dip ] when
-        pick list? [ traverse ] [ 2drop ] if
-    ] 2curry lmap ; inline recursive
+:: traverse ( list pred quot: ( list/elt -- result ) -- result )
+    list [| elt |
+        elt dup pred call [ quot call ] when
+        dup list? [ pred quot traverse ] when
+    ] lmap ; inline recursive
 
 INSTANCE: cons list
+INSTANCE: +nil+ list
index 00f257a5cffcfefeb96921326dcb292ca554b51c..bcf44601707a5bab048c6a198f9bc5ac3a8bbad3 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2009 Daniel Ehrenberg
 ! 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
 
 TUPLE: word key width break? ;