99 Bottles of Beer/Scala: Difference between revisions

Content added Content deleted
(ParRange)
(Previous version failed with line spacing)
Line 2: Line 2:
The trivial solution to it would be this:
The trivial solution to it would be this:


<lang scala>99 to 1 by -1 foreach {n =>
<lang scala>99 to 1 by -1 foreach { n =>
println("""|%d bottles of beer on the wall
println(
|%d bottles of beer
f"$n%d bottles of beer on the wall\n" +
f"$n%d bottles of beer\n" +
|Take one down, pass it around
f"Take one down, pass it around\n" +
|%d bottles of beer on the wall\n""".stripMargin format (n, n, n -1))}</lang>
f"${n - 1}%d bottles of beer on the wall\n")
}</lang>


The above running in paralell using a ParRange
The above running in parallel using a ParRange, fast but shuffles the output.


<lang scala>(99 to 1 by -1).par foreach {n =>
<lang scala>(99 to 1 by -1).par foreach { n =>
println("""|%d bottles of beer on the wall
println(
|%d bottles of beer
f"$n%d bottles of beer on the wall\n" +
f"$n%d bottles of beer\n" +
|Take one down, pass it around
f"Take one down, pass it around\n" +
|%d bottles of beer on the wall\n""".stripMargin format (n, n, n -1))}</lang>
f"${n - 1}%d bottles of beer on the wall\n")
}</lang>


A Regex solution:
A Regex solution: