   (:require (clojure [xml :as xml]))
   (:require (clojure [zip :as zip]))
   (:import (java.util.regex Pattern)))
+
 (time (let [x (future (do (Thread/sleep 5000)
                           (+ 41 1)))]
         [@x @x]))
 
 (defn feed->zipper [uri-str]
-  (->> xml/parse uri-str)
-  zip/xml-zip)
+  (->> (xml/parse uri-str)
+       zip/xml-zip))
 
 (defn normalize [feed]
   (if (= :feed (:tag (first feed)))


(defn count-text-task [extractor txt feed]
  (let [items (feed-children feed)
        re (Pattern/compile (str "(?i" txt ))]
    (->> items
         (map extractor)
         (mapcat #(re-seq re %))

 title
 "Erlang"
 "http://feeds.feedburner.com/ElixirLang")

(def feeds #{"http://feeds.feedburner.com/ElixirLang"
            "http://blog.fogus.me/feed/"})

(let [results (for [feed feeds]
                (future
                  (count-text-task title "Elixir" feed)))]
  (reduce + (map deref results)))

(defmacro as-futures [[a args] & body]
  (let [parts (partition-by #{'=>} body)
        [acts _ [res]] (partition-by #{:as} (first parts))
        [_ _ task] parts]
    `(let [~res (for [~a ~args] (future ~@acts))]
       ~@task)))

(defn occurrences [extractor tag & feeds]
  (as-futures [feed feeds]
              (count-text-task extractor tag feed)
              :as results
              =>
              (reduce + (map deref results))))

#_(occurrences title "released"
            "http://blog.fogus.me/feed/"
            "http://feeds.feedburner.com/ElixirLang"
            "http://www.ruby-lang.org/en/feeds/news.rss")
