(ns my-wide-finder
"A basic map/reduce approach to the wide finder using agents.
Optimized for being idiomatic and readable rather than speed.
NOTE: Originally from:
http://technomancy.us/130
but updated to use pmap."
(:use [clojure.contrib.duck-streams :only [reader read-lines]]))
(def re #"GET /(\d+) ")
(defn count-line
"Increment the relevant entry in the counts map."
[line]
(if-let [[_ hit] (re-find re line)]
{hit 1}
{}))
(defn count-lines
"Grab a chunk of lines and ..."
[seq]
(apply merge-with +
(map count-line (take 20 seq))))
(defn my-find-widely
"Return a map of pages to hit counts in filename."
[filename]
(apply merge-with +
(pmap count-lines (line-seq (reader filename)))))