Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain lazy Pascal's triangle in Clojure

I came across this elegant implementation of Pascal's triangle that uses a lazy sequence.

(def pascal
  (iterate
   (fn [prev-row]
     (->>
      (concat [[(first prev-row)]] (partition 2 1 prev-row) [[(last prev-row)]])
      (map (partial apply +) ,,,)))
   [1M]))

Could anyone help me understanding the ,,, in this context? I tried using macroexpand but that didn't get me far. I also know it's use is not required, but I want to know that ,,, means.

like image 912
darth10 Avatar asked Sep 23 '12 09:09

darth10


1 Answers

Commas are treated as white space in Clojure so Reader will ignore ,,, completely. The reason it is there is to make the code more readable for humans.

In this context, the ->> macro will insert the (concat ...) in the last position of the call to (map ...), i.e. in the position of ,,,.

The ,,, commonly used with -> and ->> macros to make the code more readable, but does not actually do anything.

like image 133
ddk Avatar answered Nov 07 '22 10:11

ddk