I wonder how many code bases out there have their own variant of `flip`? Here’s ours:
(defn flip "Like partial except you supply everything but the first argument." ([f b] (fn [a] (f a b))) ([f b c] (fn [a] (f a b c))) ([f b c d & more] (fn [a] (apply f a b c d more)))) Sean On Apr 30, 2015, at 4:44 PM, Ben Wolfson <wolf...@gmail.com> wrote: > Though all this saves you is a single function call, and flip is a useful > thing to have around anyway. > > btw, as far as "flip", you don't need the "into" call; you can just do: > > (defn flip [f] (fn [x y & args] (apply f y x args)) > > though I don't know how if this is actually significant. > > > On Thu, Apr 30, 2015 at 3:47 PM, Vagmi Mudumbai <m...@vagmim.in > <mailto:m...@vagmim.in>> wrote: > 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 > <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/ > > <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.