99 Bottles of Beer/Scala: Difference between revisions

m
Fixed syntax highlighting.
(moved from 99 Bottles of Beer)
 
m (Fixed syntax highlighting.)
 
(6 intermediate revisions by 5 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" +
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
<syntaxhighlight 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")
}</syntaxhighlight>
 
A ===Regex solution:===
<langsyntaxhighlight lang="scala">object NinetyNineBottlesOfBeer {
 
<lang scala>object NinetyNineBottlesOfBeer {
val verse = """|99 bottles of beer on the wall
|99 bottles of beer
Line 30 ⟶ 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 126 ⟶ 139:
Patrons ! SingSong(Beer)
}
}</langsyntaxhighlight>
9,476

edits