Hi,

I was introducing one of my colleagues to clojure[1] and we were
trying to parse the reddit json as an exercise.

(require '(clj-http.client :as client))
(require '(clojure.data.json :as json))

(def ^:const REDDIT-URL "http://reddit.com/r/clojure.json?limit=100";)
(def ^:const headers {:headers {"User-Agent" "showoffclojure.core by vagmi"}})

(let [entries_ (-> REDDIT-URL
                   (client/get headers)
                   (:body)
                   (json/read-str :key-fn keyword)
                   (:data)
                   (:children))]
  (map :data entries))

It would have been nice if we were able to write the map as a part of
the threading macro. So if there were a flip function like in haskell,
we could flip the args to the function hand have the map in the
threading macro. I could not find one so I wrote one.

(defn flip [fn_]
  (fn [x y & args]
    (apply fn_ (into [y x] args))))

Now I can bring in the map as a part of the -> threading macro.

(-> REDDIT-URL
    (client/get headers)
    (:body)
    (json/read-str :key-fn keyword)
    (:data)
    (:children)
    ((flip map) :data))

This seems to be rather easy and useful and gets rid of the let block.
Are there any implications to performance or impact to laziness by
using flip? This would be useful even on the transduced version of
map like ((flip transduce) (map :data)) on the last expression.

Regards,
Vagmi


[1]: 
(http://blog.tarkalabs.com/2015/04/30/experience-report-introducing-an-experienced-ruby-developer-to-clojure/)
and

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to