:( map is lazy, so you'll need to wrap it in doall
(dotimes [i 4] (println "Happy Birthday" ({2 "Dear XXX"} i "To You"))) On Thu, Sep 17, 2009 at 9:17 PM, David Nolen <dnolen.li...@gmail.com> wrote: > Actually to be fair, here's a Clojure version that uses as little whitespace > as the Scala and Java ones do. > (map #(str"Happy Birthday "%)(assoc (vec (replicate 4"To You"))2"Dear XXX")) > ; -> 76 chars > > On Fri, Sep 18, 2009 at 12:14 AM, David Nolen <dnolen.li...@gmail.com> > wrote: >> >> Your basic approach seems sound: >> (map #(str "Happy Birthday " %) (assoc (vec (replicate 4 "To You")) 2 >> "Dear XXX") -> 81 chars including white space >> for(int i=0;i<4;i++){System.out.println("Happy Birthday "+(i==2?"Dear >> XXX":"To You"));}) -> 88 chars >> (1 to 4).map{i=>"Happy Birthday %s".format(if(i==3)"Dear XXX"else"To >> You")}.foreach{println(_)} -> 95 chars >> Anyone have a shorter version? :) >> On Thu, Sep 17, 2009 at 11:53 PM, Wilson MacGyver <wmacgy...@gmail.com> >> wrote: >>> >>> This blog post got me thinking. >>> http://www.artima.com/weblogs/viewpost.jsp?thread=268561 >>> >>> Basically it contains both a Java one liner and Scala one liner. >>> >>> Java: >>> for(int i=0; i<4; i++) { System.out.println("Happy Birthday " + (i==2 >>> ? "Dear XXX" : "To You")); } >>> >>> Scala: >>> (1 to 4).map { i => "Happy Birthday %s".format(if (i == 3) "Dear XXX" >>> else "To You") }.foreach { println(_) } >>> >>> the goal is to generate >>> >>> Happy Birthday To You >>> Happy Birthday To You >>> Happy Birthday Dear XXX >>> Happy Birthday To You >>> >>> >>> I started thinking about how to do this in clojure. My first reaction was >>> to >>> think of the sentences as two sequences. Uses replicate to generate >>> them, and map str to join them from two collections. >>> >>> ie, (map str (replicate 4 "Happy Birthday ")... >>> >>> Is there a more "clojure" way to do it? >>> because using replicate to generate the 2nd sequence seem like cheating. >>> ie, replicate 2 "To You", 1 "Dear XXX", and then "To You" again. >>> >>> >>> >>> -- >>> Omnem crede diem tibi diluxisse supremum. >>> >>> >> > > > > > -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---