99 Bottles of Beer/Scala: Difference between revisions

Content added Content deleted
m (Avoid miscounting task entries twice)
m (Fixed syntax highlighting.)
 
Line 2: Line 2:
<span style='font-family: "Linux Libertine",Georgia,Times,serif;font-size:150%;'>[[Scala]]</span><hr>
<span style='font-family: "Linux Libertine",Georgia,Times,serif;font-size:150%;'>[[Scala]]</span><hr>


==The trivial solution==
===The trivial solution===
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 =>
<syntaxhighlight 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 on the wall\n" +
Line 10: Line 10:
f"Take one down, pass it around\n" +
f"Take one down, pass it around\n" +
f"${n - 1}%d bottles of beer on the wall\n")
f"${n - 1}%d bottles of beer on the wall\n")
}</lang>
}</syntaxhighlight>

===Running in parallel===
===Running in parallel===
The above n parallel using a ParRange, fast but shuffles the output.
The above n parallel using a ParRange, fast but shuffles the output.
<lang Scala>(99 to 1 by -1).par foreach { n =>
<syntaxhighlight lang="scala">(99 to 1 by -1).par foreach { n =>
println(
println(
f"$n%d bottles of beer on the wall\n" +
f"$n%d bottles of beer on the wall\n" +
Line 19: Line 20:
f"Take one down, pass it around\n" +
f"Take one down, pass it around\n" +
f"${n - 1}%d bottles of beer on the wall\n")
f"${n - 1}%d bottles of beer on the wall\n")
}</lang>
}</syntaxhighlight>

==Regex solution==
===Regex solution===
<lang Scala>object NinetyNineBottlesOfBeer {
<syntaxhighlight lang="scala">object NinetyNineBottlesOfBeer {
val verse = """|99 bottles of beer on the wall
val verse = """|99 bottles of beer on the wall
|99 bottles of beer
|99 bottles of beer
Line 41: Line 43:
changeLine(line)
changeLine(line)
}
}
}</lang>
}</syntaxhighlight>

==A preferred ☺ sollution ==
===A preferred ☺ solution===
<lang Scala>// Note - As Of Scala 2.11.0 The 'Scala Actors' Library Has Been Deprecated In Favor Of Akka.
<syntaxhighlight lang="scala">// Note - As Of Scala 2.11.0 The 'Scala Actors' Library Has Been Deprecated In Favor Of Akka.
object Song {
object Song {
import scala.actors._
import scala.actors._
Line 136: Line 139:
Patrons ! SingSong(Beer)
Patrons ! SingSong(Beer)
}
}
}</lang>
}</syntaxhighlight>