mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 11:09:54 +00:00
Add iter2lazy and lazy2iter
This commit is contained in:
parent
26c8f7a5cf
commit
e7fe9fdcf6
@ -62,7 +62,7 @@ body once, and then memoizes the result."
|
||||
(range2 0 end))
|
||||
|
||||
(defn map
|
||||
"Return a sequence that is the result of apply f to each value in s."
|
||||
"Return a sequence that is the result of applying f to each value in s."
|
||||
[f s]
|
||||
(delay
|
||||
(def x (s))
|
||||
@ -105,3 +105,39 @@ body once, and then memoizes the result."
|
||||
(when x
|
||||
(def thehead (get HEAD x))
|
||||
(if thehead (tuple thehead (take-while pred (get TAIL x)))))))
|
||||
|
||||
#Iterators is a conscept that looks a lot like lazy seq
|
||||
#The following functions turn iterators to lazy seq and vice versa
|
||||
|
||||
(defn- iter-self
|
||||
[next]
|
||||
(delay (tuple (next) (iter-self next))))
|
||||
|
||||
(defn iter2lazy
|
||||
"Create a lazy sequence froma an iterator"
|
||||
[iter]
|
||||
(def {:more more :next next} iter)
|
||||
(iter-self next))
|
||||
|
||||
(defn lazy2iter
|
||||
"turn a lazy-seq to an iterator"
|
||||
[lazy-seq]
|
||||
(var state lazy-seq)
|
||||
{:more state
|
||||
:next (fn [] (def result (head state) )
|
||||
(def rest (tail state))
|
||||
(if rest
|
||||
(:= state rest))
|
||||
result)})
|
||||
|
||||
|
||||
#Now we can use the nonfuctional filter from boot.dst
|
||||
#to write a filter version that returns a lazy sequence
|
||||
|
||||
|
||||
(defn filter2 [pred coll]
|
||||
(tail (iter2lazy (filter pred coll))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user