SHARE
TWEET

Untitled

a guest Dec 25th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns lab32)
  2.  
  3. (def futureCap 3)
  4.  
  5. (defn pfilter [pred coll]
  6.   (let [n 4
  7.         chunked (fn chunked [c]
  8.                   (lazy-seq
  9.                     (if (not-empty c)
  10.                       (cons (take futureCap c) (chunked (drop futureCap c))))))
  11.         fut (fn [c] (map #(future (do (println "fut" %) (doall (filter pred %)))) c))
  12.         step (fn step [[x & xs :as vs] r]
  13.                (lazy-seq
  14.                  (if-let [s (seq r)]
  15.                    (cons (deref x) (step xs (rest s)))
  16.                    (map deref vs)
  17.                    )))
  18.         fc (fut (chunked coll))]
  19.     (flatten (step fc (drop n fc)))
  20.     )
  21.   )
  22.  
  23.  
  24. (let [coll `(6 4 5 2 3 2 8 0 3 7 9 11)
  25.       pred (fn [x] (do
  26.                      (println (format "lazy%d " x))
  27.                      (Thread/sleep 5000)
  28.                      (even? x)
  29.                      ))
  30.       ]
  31.   (do
  32.     (time (doall (pfilter pred coll)))
  33.     (time (doall (filter pred coll)))
  34.     )
  35.   )
  36.  
  37. (shutdown-agents)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top