HELP: lazy-cons
{ $values { "car" { $quotation ( -- elt ) } } { "cdr" { $quotation ( -- cons ) } } { "promise" "the resulting cons object" } }
-{ $description "Constructs a cons object for a lazy list from two quotations. The " { $snippet "car" } " quotation should return the head of the list, and the " { $snippet "cons" } " quotation the tail when called. When " { $link cons } " or " { $link cdr } " are called on the lazy-cons object then the appropriate quotation is called." }
+{ $description "Constructs a cons object for a lazy list from two quotations. The " { $snippet "car" } " quotation should return the head of the list, and the " { $snippet "cons" } " quotation the tail when called. When " { $link cons } " or " { $link cdr } " are called on the lazy-cons object then the appropriate quotation is called." }
{ $see-also cons car cdr nil nil? } ;
{ 1lazy-list 2lazy-list 3lazy-list } related-words
HELP: <memoized-cons>
{ $values { "cons" "a cons object" } { "memoized-cons" "the resulting memoized-cons object" } }
-{ $description "Constructs a cons object that wraps an existing cons object. Requests for the car, cdr and nil? will be remembered after the first call, and the previous result returned on subsequent calls." }
+{ $description "Constructs a cons object that wraps an existing cons object. Requests for the car, cdr and nil? will be remembered after the first call, and the previous result returned on subsequent calls." }
{ $see-also cons car cdr nil nil? } ;
{ lazy-map ltake lfilter lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words
{ $description "Convert the sequence into a list, starting from " { $snippet "index" } "." }
{ $see-also >list } ;
-HELP: >list
-{ $values { "object" object } { "list" "a list" } }
-{ $description "Convert the object into a list. Existing lists are passed through intact, sequences are converted using " { $link sequence-tail>list } " and other objects cause an error to be thrown." }
-{ $see-also sequence-tail>list } ;
-
{ leach foldl lazy-map ltake lfilter lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words
HELP: lconcat
HELP: lmerge
{ $values { "list1" "a list" } { "list2" "a list" } { "result" "lazy list merging list1 and list2" } }
-{ $description "Return the result of merging the two lists in a lazy manner." }
+{ $description "Return the result of merging the two lists in a lazy manner." }
{ $examples
{ $example "USING: lists lists.lazy prettyprint ;" "{ 1 2 3 } >list { 4 5 6 } >list lmerge list>array ." "{ 1 4 2 5 3 6 }" }
} ;
HELP: lcontents
{ $values { "stream" "a stream" } { "result" string } }
-{ $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." }
+{ $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 llines } ;
HELP: llines
{ $values { "stream" "a stream" } { "result" "a list" } }
-{ $description "Returns a lazy list of all lines in the file. " { $link car } " returns the next lines in the file, " { $link cdr } " returns the remaining lines as a lazy list. " { $link nil? } " indicates end of file." }
+{ $description "Returns a lazy list of all lines in the file. " { $link car } " returns the next lines in the file, " { $link cdr } " returns the remaining lines as a lazy list. " { $link nil? } " indicates end of file." }
{ $see-also lcontents } ;
! Copyright (C) 2004, 2008 Chris Double, Matthew Willis, James Cash.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays combinators io kernel lists math
-promises quotations sequences summary vectors ;
+promises quotations sequences vectors ;
IN: lists.lazy
M: promise car ( promise -- car )
M: promise nil? ( cons -- ? )
force nil? ;
-
+
! Both 'car' and 'cdr' are promises
TUPLE: lazy-cons-state car cdr ;
M: sequence-cons nil? ( sequence-cons -- ? )
drop f ;
-ERROR: list-conversion-error object ;
-
-M: list-conversion-error summary
- drop "Could not convert object to list" ;
-
-: >list ( object -- list )
- {
- { [ dup sequence? ] [ 0 swap sequence-tail>list ] }
- { [ dup list? ] [ ] }
- [ list-conversion-error ]
- } cond ;
+M: sequence >list 0 swap sequence-tail>list ;
TUPLE: lazy-concat car cdr ;
over nil? [ nip lconcat ] [ <lazy-concat> ] if ;
: lconcat ( list -- result )
- dup nil? [ drop nil ] [ uncons (lconcat) ] if ;
+ dup nil? [ drop nil ] [ uncons (lconcat) ] if ;
M: lazy-concat car ( lazy-concat -- car )
car>> car ;
{ $values { "list" list } { "quot" quotation } { "array" array } }
{ $description "Executes the quotation on each element of the list, collecting the results in an array." } ;
+HELP: >list
+{ $values { "object" object } { "list" "a list" } }
+{ $description "Converts the object into a list." } ;
! Copyright (C) 2008 James Cash, Daniel Ehrenberg, Chris Double.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences accessors math arrays vectors classes words
-combinators.short-circuit combinators locals ;
+combinators.short-circuit combinators locals summary ;
IN: lists
! List Protocol
GENERIC: car ( cons -- car )
GENERIC: cdr ( cons -- cdr )
GENERIC: nil? ( object -- ? )
-
+
TUPLE: cons-state { car read-only } { cdr read-only } ;
C: cons cons-state
: 3list ( a b c -- cons ) 2list cons ; inline
: cadr ( list -- elt ) cdr car ; inline
-
+
: 2car ( list -- car caar ) [ car ] [ cadr ] bi ; inline
-
+
: 3car ( list -- car cadr caddr ) [ car ] [ cadr ] [ cdr cadr ] tri ; inline
: lnth ( n list -- elt ) swap [ cdr ] times car ; inline
[ [ unswons ] dip cons ] times
lreverse swap ;
-: sequence>list ( sequence -- list )
+: sequence>list ( sequence -- list )
<reversed> nil [ swons ] reduce ;
: lmap>array ( ... list quot: ( ... elt -- ... newelt ) -- ... array )
collector [ leach ] dip { } like ; inline
-: list>array ( list -- array )
+: list>array ( list -- array )
[ ] lmap>array ;
INSTANCE: cons-state list
INSTANCE: +nil+ list
+
+GENERIC: >list ( object -- list )
+
+M: list >list ;