Skip to content

Instantly share code, notes, and snippets.

@devnoo
Created February 21, 2012 20:37
Show Gist options
  • Select an option

  • Save devnoo/1878752 to your computer and use it in GitHub Desktop.

Select an option

Save devnoo/1878752 to your computer and use it in GitHub Desktop.
seven languages in seven weeks scala day 3
import collection.mutable.HashSet
import dbc.result.Tuple
import scala.io._
import scala.actors._
import Actor._
import scala.util.matching.Regex
val linkPattern = new Regex("""<a +href=\"([^\"]+)\"[^>]*>""", "link")
case class Page(val content: String) {
def size = content.length
def links(): Set[String] = {
linkPattern.findAllIn(content).foldLeft(Set(): Set[String]) {
(links: Set[String], link) => links + link
}
}
def numberOfLinks() {
//Should count non unique links probably
linkPattern.findAllIn(content).size
}
}
object PageLoader {
def getPage(url: String) = {
Page(Source.fromURL(url).mkString)
}
}
//removed amazon.com cause of strange bug with encoding in scala 2.9.1
val urls = List("http://www.twitter.com/",
"http://www.google.com/",
"http://www.cnn.com/")
def getPageSizeSequentially() = {
for (url <- urls) {
val page: Page = PageLoader.getPage(url)
println("Size for " + url + ": " + page.size)
println("Number of links for " + url + ": " + page.links().size)
}
}
def getPageSizeConcurrently() = {
val caller = self
for (url <- urls) {
actor {
caller !(url, PageLoader.getPage(url))
}
}
for (i <- 1 to urls.size) {
receive {
case (url, page: Page) =>
println("Size for " + url + ": " + page.size)
println("Number of links for " + url + ": " + page.links().size)
}
}
}
def timeMethod(method: () => Unit) = {
val start = System.nanoTime
method()
val end = System.nanoTime
println("Method took " + (end - start) / 1000000000.0 + " seconds.")
}
println("Sequential run:")
timeMethod {
getPageSizeSequentially
}
println("Concurrent run")
timeMethod {
getPageSizeConcurrently
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment