->> and -> actually play nice with each other in this direction:

(-> 4
      range
      (->> (map inc))
      last)

results in 4, because we end up with (-> (range 4) (->> (map inc)) last)
--> (-> (->> (range 4) (map inc)) last) --> (last (->> (range 4) (map
inc))) --> (last (map inc (range 4))). 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> 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.
>



-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
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