Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(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)))))