mirror of
https://github.com/janet-lang/janet
synced 2025-01-13 00:50:26 +00:00
Improve lazyseq
This commit is contained in:
parent
e7fe9fdcf6
commit
986c1764ef
@ -122,22 +122,24 @@ body once, and then memoizes the result."
|
|||||||
(defn lazy2iter
|
(defn lazy2iter
|
||||||
"turn a lazy-seq to an iterator"
|
"turn a lazy-seq to an iterator"
|
||||||
[lazy-seq]
|
[lazy-seq]
|
||||||
(var state lazy-seq)
|
(var result (head lazy-seq) )
|
||||||
{:more state
|
(var rest (tail lazy-seq))
|
||||||
:next (fn [] (def result (head state) )
|
{:more (fn [] result)
|
||||||
(def rest (tail state))
|
:next (fn [] (def next result)
|
||||||
(if rest
|
(when result
|
||||||
(:= state rest))
|
(:= result (head rest))
|
||||||
result)})
|
(:= rest (tail rest)))
|
||||||
|
next)})
|
||||||
|
|
||||||
|
|
||||||
#Now we can use the nonfuctional filter from boot.dst
|
#Now we can use the nonfuctional filter from boot.dst
|
||||||
#to write a filter version that returns a lazy sequence
|
#to write a filter version that returns a lazy sequence
|
||||||
|
#Be carefull when creating lazy sequences from mutable
|
||||||
|
#data structures as their values are references to this
|
||||||
|
#data structures. Same is true for iterators
|
||||||
|
|
||||||
(defn filter2 [pred coll]
|
(defn filter2 [pred coll]
|
||||||
(tail (iter2lazy (filter pred coll))))
|
(tail (iter2lazy (filter pred coll))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user