Created
May 14, 2012 21:22
-
-
Save mduvall/2697224 to your computer and use it in GitHub Desktop.
Various examples for Scala discussion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.actors._ | |
import scala.actors.Actor._ | |
case object Poke | |
case object Feed | |
class Kid() extends Actor { | |
def act() { | |
loop { | |
react { | |
case Poke => { | |
println("Ow...") | |
println("Quit it...") | |
} | |
case Feed => { | |
println("Gurgle...") | |
println("Burp...") | |
} | |
} | |
} | |
} | |
} | |
val bart = new Kid().start | |
val lisa = new Kid().start | |
println("Ready to poke and feed...") | |
bart ! Poke | |
lisa ! Poke | |
bart ! Feed | |
lisa ! Feed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.io._ | |
import scala.actors._ | |
import Actor._ | |
// START:loader | |
object PageLoader { | |
def getPageSize(url : String) = Source.fromURL(url).mkString.length | |
} | |
// END:loader | |
val urls = List("http://www.amazon.com/", | |
"http://www.twitter.com/", | |
"http://www.google.com/", | |
"http://www.cnn.com/" ) | |
// START:time | |
def timeMethod(method: () => Unit) = { | |
val start = System.nanoTime | |
method() | |
val end = System.nanoTime | |
println("Method took " + (end - start)/1000000000.0 + " seconds.") | |
} | |
// END:time | |
// START:sequential | |
def getPageSizeSequentially() = { | |
for(url <- urls) { | |
println("Size for " + url + ": " + PageLoader.getPageSize(url)) | |
} | |
} | |
// END:sequential | |
// START:concurrent | |
def getPageSizeConcurrently() = { | |
val caller = self | |
for(url <- urls) { | |
actor { caller ! (url, PageLoader.getPageSize(url)) } | |
} | |
for(i <- 1 to urls.size) { | |
receive { | |
case (url, size) => | |
println("Size for " + url + ": " + size) | |
} | |
} | |
} | |
// END:concurrent | |
// START:script | |
println("Sequential run:") | |
timeMethod { getPageSizeSequentially } | |
println("Concurrent run") | |
timeMethod { getPageSizeConcurrently } | |
// END:script |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person(val name: String) { | |
def talk(message: String) = println(name + " says " + message) | |
def id(): String = name | |
} | |
class Employee(override val name: String, val number: Int) extends Person(name) { | |
override def talk(message: String) { | |
println(name + " with number " + number + " says " + message) | |
} | |
override def id():String = number.toString | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val movies = | |
<movies> | |
<movie genre="sci-fi">Titanic</movie> | |
<movie genre="comedy">Alien</movie> | |
</movies> | |
println(movies) | |
println("----------------------") | |
println(movies.text) | |
println("----------------------") | |
println((movies \ "movie")(0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def doChore(chore: String): String = chore match { | |
case "clean dishes" => "scrub, dry" | |
case "cook dinner" => "chop, sizzle" | |
case _ => "whine, complain" | |
} | |
println(doChore("clean dishes")) | |
println(doChore("mow lawn")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def quickSort: List[Int] => List[Int] = { | |
case Nil => Nil | |
case pivot :: tail => | |
val (smaller, rest) = tail.partition(_ < pivot) | |
quickSort(smaller) ::: pivot :: quickSort(rest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment