Advertisement
Guest User

Untitled

a guest
Dec 25th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns lab32)
  2.  
  3. (defn pfilter [pred coll]
  4.   (let [n 2
  5.         future-cap 4
  6.         flat (fn flat [coll]
  7.                (lazy-seq
  8.                  (when-let [s (seq coll)]
  9.                    (if (coll? (first s))
  10.                      (concat (flat (first s)) (flat (rest s)))
  11.                      (cons (first s) (flat (rest s)))))))
  12.         chunked (fn chunked [c]
  13.                   (lazy-seq
  14.                     (if (not-empty c)
  15.                       (cons (take future-cap c) (chunked (drop future-cap c))))))
  16.         fut (fn [c] (doall (filter pred c)))
  17.         fs (map #(future (fut %)) (chunked coll))
  18.         step (fn step [[x & xs :as vs] tail]
  19.                (lazy-seq
  20.                  (if-let [s (seq tail)]
  21.                    (cons (deref x) (step xs (rest s)))
  22.                    (map deref vs)
  23.                    )))]
  24.     (flat (step fs (drop (dec n) fs)))
  25.     )
  26.   )
  27.  
  28.  
  29. (let [coll (range 20)
  30.       pred (fn [x] (do
  31.                      (println (format "lazy%d " x))
  32.                      (Thread/sleep 1000)
  33.                      (even? x)
  34.                      ))
  35.       ]
  36.   (do
  37.     (time (doall (pfilter pred coll)))
  38.     ;(time (pfilter pred coll))
  39.     ;(time (doall (take 1 (pmap pred coll))))
  40.     ;(time (doall (take 1 (pfilter pred coll))))
  41.     ;(time (doall (filter pred coll)))
  42.     ;(time (pfilter pred coll))
  43.     ;(time (pmap pred coll))
  44.     )
  45.   )
  46.  
  47.  
  48. (shutdown-agents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement