]> gitweb.factorcode.org Git - factor.git/commitdiff
lazy-lists: make lconcat lazy
authorchris.double <chris.double@double.co.nz>
Mon, 9 Oct 2006 01:23:06 +0000 (01:23 +0000)
committerchris.double <chris.double@double.co.nz>
Mon, 9 Oct 2006 01:23:06 +0000 (01:23 +0000)
contrib/lazy-lists/lists.factor

index 4cc9073f302df4bdfaa47d954f2afea5df50d1b9..26fcfd9fba7f76460f995952a799fbe81db14de4 100644 (file)
@@ -371,8 +371,39 @@ M: sequence-cons list? ( object -- bool )
     { [ t ] [ "Could not convert object to a list" throw ] }
   } cond ;
 
-: lconcat ( list -- result )
-  list>array nil [ lappend ] reduce ;
+TUPLE: lazy-concat car cdr ;
+
+DEFER: lconcat
+
+: (lconcat) ( car cdr -- list )
+  over nil? [
+    nip lconcat 
+  ] [
+    <lazy-concat>    
+  ] if ;
+  
+: lconcat ( list -- list )
+  dup nil? [
+    drop nil
+  ] [
+    uncons (lconcat)
+  ] if ;
+
+M: lazy-concat car ( lazy-concat -- car )
+  lazy-concat-car car ;
+
+M: lazy-concat cdr ( lazy-concat -- cdr )
+  [ lazy-concat-car cdr ] keep lazy-concat-cdr (lconcat) ;
+
+M: lazy-concat nil? ( lazy-concat -- bool )
+  dup lazy-concat-car nil? [
+    lazy-concat-cdr nil?
+  ] [
+    drop f
+  ] if ;
+
+M: lazy-concat list? ( object -- bool )
+  drop t ;
 
 : lcartesian-product ( list1 list2 -- result ) 
   swap [ swap [ 2array ] lmap-with ] lmap-with lconcat ;