Problem #71: Rearranging Code ->

35 views
Skip to first unread message

David Wagle

unread,
Nov 6, 2017, 1:40:36 PM11/6/17
to 4Clojure
The problem is: 

The -> macro threads an expression x through a variable number of forms. First, x is inserted as the second item in the first form, making a list of it if it is not a list already. Then the first form is inserted as the second item in the second form, making a list of that form if necessary. This process continues for all the forms. Using -> can sometimes make your code more readable.
test not run
(= (__ (sort (rest (reverse [2 5 4 1 3 6]))))
   (-> [2 5 4 1 3 6] (reverse) (rest) (sort) (__))
   5)






Can someone put this description into an understandable format? They all look like English words, but I have no idea what the heck they mean in this context. 

Can someone explain what "->" does in a simpler fashion? 

Bill Allen

unread,
Nov 6, 2017, 3:20:57 PM11/6/17
to 4clo...@googlegroups.com
The easiest way to think about it is from left to right the result of the previous operation becomes the parameter to the next.

In this case the list [2 5 4 1 3 6] gets inserted as the parameter of reverse. The result of that is inserted as parameter of rest and so on. This is generally thought of as more readable than the traditional nested lisp style.
--
You received this message because you are subscribed to the Google Groups "4Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Wagle

unread,
Nov 6, 2017, 3:22:06 PM11/6/17
to 4clo...@googlegroups.com
Oh, ok. tthat’s much easier to understand. Thanks!
You received this message because you are subscribed to a topic in the Google Groups "4Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/4clojure/hvi6RKyx5aE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to 4clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages