tag:blogger.com,1999:blog-12467669.post2792758514334080851..comments2023-04-29T07:23:25.825-04:00Comments on Jay Fields' Thoughts: Clojure: Refactoring From Thread Last (->>) To Thread First (->)Jayhttp://www.blogger.com/profile/14491442812573747680noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-12467669.post-13830282966355016242012-11-06T12:18:10.298-05:002012-11-06T12:18:10.298-05:00In situations where functions in a threading form ...In situations where functions in a threading form take arguments in an irregular order, the following macro from "Clojure in Action" book is useful:<br /><br />(defmacro thread-it [& [first-expr & rest-exprs]]<br /> (if (empty? rest-exprs)<br /> first-expr<br /> `(let [~'it ~first-expr]<br /> (thread-it ~@rest-exprs))))<br /><br />With this macro the example looks like this:<br /><br />(thread-it <br /> [jay john mike chris]<br /> (filter (comp (partial = "new york") :current-city) it)<br /> (group-by :employer it)<br /> (update-in it ["drw.com"] (partial map :name)))<br /><br />We can also name it, for example, >> (https://gist.github.com/4026073).Vasilyhttps://www.blogger.com/profile/08538100460991899296noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-5294688106295081042012-09-28T18:08:35.908-04:002012-09-28T18:08:35.908-04:00Interesting post. I tried the challenge myself but...Interesting post. I tried the challenge myself but without using -> or ->> (which I also like, but here I prefered to work without them):<br /><br />=> (let [people [jay john mike chris]<br /> => new-yorkers (filter #(= "new york" (:current-city %)) people)<br /> => by-employer (group-by :employer new-yorkers)]<br /> => (assoc by-employer "drw.com" (map :name (get by-employer "drw.com"))))<br />{"drw.com" ("jay fields" "john dydo"), "thoughtworks.com" [{:name "chris george", :current-city "new york", :employer "thoughtworks.<br />com"}]}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-27251395331442823852012-09-28T14:54:16.237-04:002012-09-28T14:54:16.237-04:00Cool stuff! Posted a comment in the gist :)Cool stuff! Posted a comment in the gist :)Marxamahttps://www.blogger.com/profile/05449476213967958725noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-26501197135743727412012-09-28T14:53:29.221-04:002012-09-28T14:53:29.221-04:00This comment has been removed by the author.Marxamahttps://www.blogger.com/profile/05449476213967958725noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-57979194288196823832012-09-28T11:29:39.190-04:002012-09-28T11:29:39.190-04:00thanks alex. I added another comment that shows ho...thanks alex. I added another comment that shows how things could be done in one pass with clojure.set.<br /><br />Cheers, JayJayhttps://www.blogger.com/profile/14491442812573747680noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-48622814853715075592012-09-28T10:24:48.564-04:002012-09-28T10:24:48.564-04:00I have really appreciated this and the previous bl...I have really appreciated this and the previous blog post.<br /><br />In the spirit of your previous blog post I had a go at re-imagining the example with clojure.set: https://gist.github.com/3799887Alex Stoddardhttps://www.blogger.com/profile/00700602554438046007noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-62140984504390182012-09-28T09:52:56.715-04:002012-09-28T09:52:56.715-04:00This comment has been removed by the author.Alex Stoddardhttps://www.blogger.com/profile/00700602554438046007noreply@blogger.com