from https://github.com/rplevy/mostly-useful (not updated in a while)
(defn flip "given a function, create a flipped 2-argument function" [f] (fn [a b] (f b a))) (defmacro flop "create a version of a function with a modified arity as specified by a vector of zero-indexed positions, e.g. [0 3 1 2]" [f positions] (let [syms (vec (repeatedly (count positions) gensym))] `(fn [~@syms] (~f ~@(map syms positions))))) On Thu, Apr 30, 2015 at 5:49 PM, Jony Hudson <jonyepsi...@gmail.com> wrote: > Another option is to use `as->` > https://clojuredocs.org/clojure.core/as-%3E . It's more verbose ... I > kind of like that aspect of it, but it may not be to everyone's taste. > > > Jony > > > On Friday, 1 May 2015 00:31:05 UTC+1, Vagmi Mudumbai 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") >> (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. > -- 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.