Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns lab32)
- (defn pfilter [pred coll]
- (let [n 2
- future-cap 4
- flat (fn flat [coll]
- (lazy-seq
- (when-let [s (seq coll)]
- (if (coll? (first s))
- (concat (flat (first s)) (flat (rest s)))
- (cons (first s) (flat (rest s)))))))
- chunked (fn chunked [c]
- (lazy-seq
- (if (not-empty c)
- (cons (take future-cap c) (chunked (drop future-cap c))))))
- fut (fn [c] (doall (filter pred c)))
- fs (map #(future (fut %)) (chunked coll))
- step (fn step [[x & xs :as vs] tail]
- (lazy-seq
- (if-let [s (seq tail)]
- (cons (deref x) (step xs (rest s)))
- (map deref vs)
- )))]
- (flat (step fs (drop (dec n) fs)))
- )
- )
- (let [coll (range 20)
- pred (fn [x] (do
- (println (format "lazy%d " x))
- (Thread/sleep 1000)
- (even? x)
- ))
- ]
- (do
- (time (doall (pfilter pred coll)))
- ;(time (pfilter pred coll))
- ;(time (doall (take 1 (pmap pred coll))))
- ;(time (doall (take 1 (pfilter pred coll))))
- ;(time (doall (filter pred coll)))
- ;(time (pfilter pred coll))
- ;(time (pmap pred coll))
- )
- )
- (shutdown-agents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement