Advertisement
Guest User

Ross Thomas

a guest
Nov 13th, 2009
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.01 KB | None | 0 0
  1. (use 'clojure.contrib.duck-streams)
  2.  
  3. ;; Count instances of strings found in a file, parallelized using agents.
  4. ;;
  5. ;; See http://www.tbray.org/ongoing/When/200x/2009/11/11/Clojure-References
  6. ;;
  7. ;; - Ross Thomas <halfacanuck@gmail.com>
  8.  
  9. (def file-name "/etc/dictionaries-common/words")
  10. (def re #"^..(...)")
  11. (def top-n 10)
  12. (def num-agents 100)
  13.  
  14. ;; Define work-seq as lazy sequence of [line agent]
  15. (def lines (read-lines file-name))
  16.  
  17. (def agent-pool (take num-agents (repeatedly #(agent {}))))
  18.  
  19. (def agents (cycle agent-pool))
  20.  
  21. (def work-seq (map vector lines agents))
  22.  
  23. ;; Find match and update agent's state
  24. (defn do-line [so-far line]
  25.   (if-let [[_ hit] (re-find re line)]
  26.     (assoc so-far hit (inc (get so-far hit 0)))
  27.     so-far))
  28.  
  29. ;; Send work to agents, wait for them to finish
  30. (doseq [[l a] work-seq] (send a do-line l))
  31.  
  32. (doseq [a agent-pool] (await a))
  33.  
  34. ;; Produce final map
  35. (def result (apply merge-with + (map deref agent-pool)))
  36.  
  37. (println (take top-n (reverse (sort-by val result))))
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement