On Dec 24, 4:20 pm, Mibu <mibu.cloj...@gmail.com> wrote:
> I'd write it this way:
> (apply + (mapcat #(range 1 %) (range 2 14)))
>
> I think idiomatically I would have written it with (partial range 1)
> instead of #(range 1 %), but I prefer compact forms.

In Clojure (anybody correct me if I'm wrong) I think it's preferable
for performance reasons to use reduce instead of apply when you can.
Also, for clarity I think it's much cleaner to have the natural range
instead of one that's been pre-mangled to suit (the numbers "two" and
"fourteen" do not immediately pop in to my head when I think of the
"Twelve Days of Christmas" ;-):

    (reduce + (mapcat #(range 1 (inc %)) (range 1 (inc 12))))

With the above usage of range, it's much easier to see the pattern in
the solution. Every once in a while I wish for an "inclusive-
range" (irange, perhaps) so I wouldn't need the increment but it's
fairly natural to read it as "range from 1 INCluding 12".

-A.
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to