99 Bottles of Beer/Scala: Difference between revisions

m
Fixed syntax highlighting.
(ParRange)
m (Fixed syntax highlighting.)
 
(5 intermediate revisions by 4 users not shown)
Line 1:
{{collection|99 Bottles of Beer}}
<span style='font-family: "Linux Libertine",Georgia,Times,serif;font-size:150%;'>[[Scala]]</span><hr>
 
===The trivial solution===
The trivial solution to it would be this:
<langsyntaxhighlight lang="scala">99 to 1 by -1 foreach { n =>
println(
println("" f"|$n%d bottles of beer on the wall\n" +
|f"$n%d bottles of beer\n" +
|f"Take one down, pass it around\n" +
println("" f"|${n - 1}%d bottles of beer on the wall\n")
}</syntaxhighlight>
 
===Running in parallel===
<lang scala>99 to 1 by -1 foreach {n =>
The above n parallel using a ParRange, fast but shuffles the output.
println("""|%d bottles of beer on the wall
<langsyntaxhighlight lang="scala">(99 to 1 by -1).par foreach { n =>
|%d bottles of beer
println(
|Take one down, pass it around
|f"$n%d bottles of beer on the wall\n""".stripMargin format (n, n, n -1))}</lang>+
|f"$n%d bottles of beer\n" +
|f"Take one down, pass it around\n" +
f"${n - |1}%d bottles of beer on the wall\n""".stripMargin format (n, n, n -1))}</lang>
}</syntaxhighlight>
 
A ===Regex solution:===
The above running in paralell using a ParRange
<langsyntaxhighlight lang="scala">object NinetyNineBottlesOfBeer {
 
<lang scala>(99 to 1 by -1).par foreach {n =>
println("""|%d bottles of beer on the wall
|%d bottles of beer
|Take one down, pass it around
|%d bottles of beer on the wall\n""".stripMargin format (n, n, n -1))}</lang>
 
A Regex solution:
 
<lang scala>object NinetyNineBottlesOfBeer {
val verse = """|99 bottles of beer on the wall
|99 bottles of beer
Line 38 ⟶ 43:
changeLine(line)
}
}</langsyntaxhighlight>
 
However, I much prefer the following:
 
===A preferred ☺ solution===
<lang scala>object Song {
<syntaxhighlight lang="scala">// Note - As Of Scala 2.11.0 The 'Scala Actors' Library Has Been Deprecated In Favor Of Akka.
<lang scala>object Song {
import scala.actors._
import scala.actors.Actor._
Line 134 ⟶ 139:
Patrons ! SingSong(Beer)
}
}</langsyntaxhighlight>
9,476

edits