]> gitweb.factorcode.org Git - factor.git/commitdiff
lazy-lists: add lazy reading of characters from a file
authorchris.double <chris.double@double.co.nz>
Sun, 17 Sep 2006 23:59:06 +0000 (23:59 +0000)
committerchris.double <chris.double@double.co.nz>
Sun, 17 Sep 2006 23:59:06 +0000 (23:59 +0000)
contrib/lazy-lists/lazy-io.factor [new file with mode: 0644]
contrib/lazy-lists/lazy-io.facts [new file with mode: 0644]
contrib/lazy-lists/load.factor

diff --git a/contrib/lazy-lists/lazy-io.factor b/contrib/lazy-lists/lazy-io.factor
new file mode 100644 (file)
index 0000000..b24e042
--- /dev/null
@@ -0,0 +1,34 @@
+! Copyright (C) 2006 Chris Double.
+! See http://factorcode.org/license.txt for BSD license.
+
+USING: kernel lazy-lists io ;
+IN: lazy-lists
+
+TUPLE: lazy-contents stream car cdr ;
+
+: lcontents ( stream -- result )
+  f f <lazy-contents> ;
+
+M: lazy-contents car ( lazy-contents -- car )
+  dup lazy-contents-car dup [
+    nip  
+  ] [ 
+    drop dup lazy-contents-stream stream-read1 
+    swap dupd set-lazy-contents-car
+  ] if ;
+
+M: lazy-contents cdr ( lazy-contents -- cdr )
+  dup lazy-contents-cdr dup [
+    nip
+  ] [
+    drop dup
+    [ lazy-contents-stream ] keep
+    car [
+      lcontents [ swap set-lazy-contents-cdr ] keep
+    ] [
+      2drop nil
+    ] if 
+  ] if ;
+
+M: lazy-contents nil? ( lazy-contents -- bool )
+  car not ;
\ No newline at end of file
diff --git a/contrib/lazy-lists/lazy-io.facts b/contrib/lazy-lists/lazy-io.facts
new file mode 100644 (file)
index 0000000..595887b
--- /dev/null
@@ -0,0 +1,10 @@
+! Copyright (C) 2006 Chris Double.
+! See http://factorcode.org/license.txt for BSD license.
+
+USING: help lazy-lists sequences ;
+
+HELP: lcontents
+{ $values { "stream" "a stream" } }
+{ $description "Returns a lazy list of all characters in the file. " { $link car } " returns the next character in the file, " { $link cdr } " returns the remaining characters as a lazy list. " { $link nil? } " indicates end of file." } 
+{ $see-also leach lmap ltake lsubset lfrom lfrom-by } ;
+
index 967d22525c5c7560083a1a6834a3a27913d74a40..536d2c1ae5b5e2c7f24dc9b46c5f37b70add9008 100644 (file)
@@ -4,6 +4,8 @@
 PROVIDE: contrib/lazy-lists { 
     "lists.factor"
     "lists.facts"
+    "lazy-io.factor"
+    "lazy-io.facts"
     "examples.factor"
 } {
     "test/lists.factor"