On Sep 18, 5:53 am, Wilson MacGyver <[email protected]> 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.

I'd probably just do the scala version :-)

(map #(printf "Happy Birthday %s\n" (if (= 2 %) "Dear XXX" "To You"))
(range 4))

But remember that the sequence is lazy, so type it directly into repl
or wrap in doall.

/Karl
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to