99 Bottles of Beer/Scala: Difference between revisions

m
Fixed syntax highlighting.
m (Rm Scala imp category)
m (Fixed syntax highlighting.)
 
(2 intermediate revisions by 2 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>
{{libheader|Scala}}
 
===The trivial solution===
The trivial solution to it would be this:
<langsyntaxhighlight lang="scala">99 to 1 by -1 foreach { n =>
println(
f"$n%d bottles of beer on the wall\n" +
Line 10:
f"Take one down, pass it around\n" +
f"${n - 1}%d bottles of beer on the wall\n")
}</langsyntaxhighlight>
 
===Running in parallel===
The above n parallel using a ParRange, fast but shuffles the output.
<langsyntaxhighlight Scalalang="scala">(99 to 1 by -1).par foreach { n =>
println(
f"$n%d bottles of beer on the wall\n" +
Line 19 ⟶ 20:
f"Take one down, pass it around\n" +
f"${n - 1}%d bottles of beer on the wall\n")
}</langsyntaxhighlight>
 
===Regex solution===
<langsyntaxhighlight Scalalang="scala">object NinetyNineBottlesOfBeer {
val verse = """|99 bottles of beer on the wall
|99 bottles of beer
Line 41 ⟶ 43:
changeLine(line)
}
}</langsyntaxhighlight>
 
===A preferred ☺ sollution 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 135 ⟶ 139:
Patrons ! SingSong(Beer)
}
}</langsyntaxhighlight>
9,476

edits