Permalink
Browse files

Merge branch 'master' of github.com:jelmstrom/sevenlanguages_at_avega

  • Loading branch information...
2 parents 07766f8 + 2deaec0 commit e2dbbc01aa6bd5fb645c799f683cb55f167a573c @jelmstrom committed Apr 25, 2012
@@ -0,0 +1,14 @@
+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
+}
+
+val employee = new Employee("Yoda", 4)
+employee.talk("Extend or extend not. There is no try.")
View
@@ -0,0 +1,10 @@
+class Person(val name:String)
+
+trait Nice {
+ def greet() = println("Howdily doodily.")
+}
+
+class Character(override val name:String) extends Person(name) with Nice
+
+val flanders = new Character("Ned")
+flanders.greet
View
@@ -0,0 +1,30 @@
+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
@@ -0,0 +1,166 @@
+import java.io.{IOException, FileNotFoundException}
+import scala.io._
+import scala.actors._
+import Actor._
+import util.matching.Regex.MatchIterator
+
+// START:loader
+object PageLoader {
+ //gjwl.href="/search?"+a+"&cad=h";return 1
+ // b1 href="http://www.google.se/imghp?hl=sv&tab=wi">Bilder</a> <a class=gb1 href="http:/
+ // href="http://www.arla.se" bla bla href="http://www.amazon.com/access"> and som
+ // href="/relative/path.html" och lite till href="http://my.com"
+ // <link rel="stylesheet" type="text/css" href="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/accessoriesCSS/US/combined-3689044428._V189697042_.css" /><script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/tmmJS/tmmJS-combined-core-65345._V1_.js" /> <script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/twister/beta/twister-dpf.cc9fb73adcb35a017570bfa9a4964009._V1_.js"/> <script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/cmuAnnotations/cmuAnnotations-cmuAnnotations-49800._V1_.js" /> <script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/accessoriesJS/accessoriesJS-accessories-49340._V1_.js" /> <script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/lazyLoadLib/lazyLoadLib-lazyLoadLib-1454._V1_.js" /> <script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/nav2/gamma/priceformatterJQ/priceformatterJQ-price-21701._V1_.js" /> <script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/x-locale/communities/profile/customer-popover/script-13-min._V224617671_.js" /> <script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/x-locale/personalization/yourstore/js/ratings-bar-366177._V204593665_.js" />');
+ //val HREF_REGEXP = """href=\s*\"(\S+)\"""".r
+ val HREF_REGEXP = """href=\s*\"(\S+)\"""".r
+
+ def getPageSize(url: String) = try {
+ Source.fromURL(url).mkString.length
+ }
+ catch {
+ case e: FileNotFoundException => 0
+ case e: IOException => 0
+ }
+
+ def getNumberOfPageLinks(url: String): Int = {
+ val foundMatches: MatchIterator = HREF_REGEXP.findAllIn(Source.fromURL(url).mkString)
+ foundMatches.size
+ }
+
+ private def extractUrlsFromHrefs(listOfHrefs : List[String]): List[String] = {
+ println("List: " + listOfHrefs.length)
+ listOfHrefs.map(href => href.split("href=\"")(1)).map(remaining => remaining.substring(0, remaining.length - 1))
+ }
+
+ def getListOfLinks(url: String): List[String] = {
+ val pageText = Source.fromURL(url).mkString
+ //println(pageText)
+ val foundHrefs: MatchIterator = HREF_REGEXP.findAllIn(pageText)
+ extractUrlsFromHrefs(foundHrefs.toList)
+ }
+}
+
+// END:loader
+
+//val urls = List("http://www.amazon.com/",
+// "http://www.twitter.com/",
+// "http://www.google.com/",
+// "http://www.cnn.com/" )
+
+//val urls = List("http://www.google.com/")
+val urls = List("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)
+ }
+ }
+}
+
+// START:concurrent
+def getTotalPageSizeConcurrently() = {
+ val caller = self
+ var totalSize = 0
+ for (url <- urls) {
+ actor {
+ caller !(url, PageLoader.getListOfLinks(url))
+ }
+ }
+
+ for (i <- 1 to urls.size) {
+ receive {
+ case (url, linkUrls: List[String]) =>
+ val filteredUrls = linkUrls.filter(link => link.startsWith("http"))
+ println("Recieved: " + filteredUrls)
+ filteredUrls.foreach {
+ linkUrl =>
+ println("Starting new Actor")
+ actor {
+ caller !(linkUrl, PageLoader.getPageSize(linkUrl))
+ }
+ }
+ for (j <- 1 to filteredUrls.size) {
+ receiveWithin(20000) {
+ case (url, size: Int) =>
+ println("Size for " + url + ": " + size)
+ totalSize = totalSize + size
+ }
+ println("Total size for all linked pages: " + totalSize)
+ }
+ }
+ }
+}
+
+def getTotalPageSizeSequencially() = {
+ var totalSize = 0
+ for (url <- urls) {
+ val linkUrls = PageLoader.getListOfLinks(url)
+ linkUrls.filter(link => link.startsWith("http")).foreach {
+ linkUrl =>
+ totalSize = totalSize + PageLoader.getPageSize(linkUrl)
+ }
+ }
+ println("Total size: " + totalSize)
+}
+
+// START:concurrent
+def getNumberOfLinksInPage() = {
+ val caller = self
+
+ for (url <- urls) {
+ actor {
+ caller !(url, PageLoader.getNumberOfPageLinks(url))
+ }
+ }
+
+ for (i <- 1 to urls.size) {
+ receive {
+ case (url, size) =>
+ println("Number of links for " + url + ": " + size)
+ }
+ }
+}
+// END:concurrent
+
+// START:script
+//PageLoader.getPageLink("http://www.amazon.com")
+println("Sequential run:")
+timeMethod { getTotalPageSizeSequencially }
+
+println("Concurrent run")
+timeMethod { getTotalPageSizeConcurrently }
+
+println("Sequential run:")
+timeMethod { getTotalPageSizeSequencially }
+
+//PageLoader.getListOfLinks("http://www.amazon.com").foreach(url => println(url))
+//timeMethod { getPageSizeConcurrently }
+// END:script
@@ -0,0 +1,13 @@
+val movies = <movies>
+ <movie>The Incredibles</movie>
+ <movie>WALL E</movie>
+ <short>Jack Jack Attack</short>
+ <short>Geri's Game</short>
+</movies>
+
+(movies \ "_").foreach { movie =>
+ movie match {
+ case <movie>{movieName}</movie> => println(movieName)
+ case <short>{shortName}</short> => println(shortName + " (short)")
+ }
+}
@@ -0,0 +1,58 @@
+import collection.immutable.Range.Int
+import io._
+import scala.actors._
+import Actor._
+
+// START:loader
+object PageLoader {
+ // If you get "MalformedInputException: Input length = 1" then you can change to fromURL(url, Codec.ISO8859.name())
+ 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

0 comments on commit e2dbbc0

Please sign in to comment.