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
object ServiceLocatorTest extends App { | |
trait SimpleServiceLocator { | |
import scala.collection.immutable.HashMap | |
private var registry = new HashMap[String,Any]() | |
def instance[T](implicit manifest: Manifest[T]): Option[T] = { | |
for { |
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 java.lang.Thread | |
import scala.collection.mutable.Queue | |
import akka.actor.Uuid | |
import akka.actor.scala2ActorRef | |
import akka.actor.Actor | |
import akka.event.EventHandler | |
/* | |
* Sleeping Barber Code Club Problem | |
* See (http://en.wikipedia.org/wiki/Sleeping_barber_problem) |
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
[akka:event-driven:dispatcher:global-3] [Customer] cust1 Enters Shop | |
[akka:event-driven:dispatcher:global-4] [Barber] Cutting Hair | |
[akka:event-driven:dispatcher:global-3] [Customer] cust1 Sits In Barber Chair | |
[akka:event-driven:dispatcher:global-7] [Customer] cust2 Enters Shop | |
[akka:event-driven:dispatcher:global-9] [Customer] cust2 Sits In Waiting Room | |
[akka:event-driven:dispatcher:global-12] [Customer] cust3 Enters Shop | |
[akka:event-driven:dispatcher:global-12] [Customer] cust3 Waiting Room Full - Leaving | |
[akka:event-driven:dispatcher:global-12] [Customer] cust3 Exits Shop | |
[akka:event-driven:dispatcher:global-4] [Barber] Cutting Hair Done | |
[akka:event-driven:dispatcher:global-18] [Customer] cust1 Exits Shop |
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
/* | |
* S99 Problem 3 | |
* D Holbrook | |
*/ | |
object P03 extends App { | |
@scala.annotation.tailrec | |
def nThRecursive[A](n: Int, l: List[A]): A = (n, l) match { | |
case (x , _) if x < 0 => throw new IllegalArgumentException //should never have negative number | |
case (0, hd :: _) => hd //counter is zero, return head |
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.annotation.tailrec | |
object Palindrome extends App { | |
//don't want to use name "reverse" because of conflict with built-in | |
def rev[A](lst: List[A]): List[A] = { | |
@tailrec | |
def loop(rlst: List[A], accumulator: List[A]): List[A] = rlst match { | |
case Nil => accumulator | |
case head :: tail => loop(tail, head :: accumulator) |
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
scala> //Eliminate consecutive duplicates of list elements. | |
scala> val l = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e) | |
l: List[Symbol] = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e) | |
scala> l.foldLeft(List[Symbol]()){ | |
| case (acc,s) if acc.isEmpty => s :: acc | |
| case (acc,s) if s == acc.head => acc | |
| case (acc,s) => s :: acc | |
| }.reverse |
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
package scalaninetynine | |
/* | |
* S99 P07 http://aperiodic.net/phil/scala/s-99/ | |
* | |
* Flatten a nested list structure. | |
* | |
* Example: | |
* scala> flatten(List(List(1, 1), 2, List(3, List(5, 8)))) | |
* res0: List[Any] = List(1, 1, 2, 3, 5, 8) | |
* |
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
package scalaninetynine | |
/* | |
* S99 P08 http://aperiodic.net/phil/scala/s-99/ | |
* | |
* If a list contains repeated elements they should be replaced with a | |
* single copy of the element. The order of the elements should not be changed. | |
* | |
* Example: | |
* scala> compress(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) | |
* res0: List[Symbol] = List('a, 'b, 'c, 'a, 'd, 'e) |
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
package scalaninetynine | |
/* | |
* S99 P09 http://aperiodic.net/phil/scala/s-99/ | |
* If a list contains repeated elements they should be placed in separate | |
* sublists. | |
* | |
* Example: | |
* scala> pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) | |
* res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e, 'e, 'e)) |
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
package scalaninetynine | |
/* | |
* S99 P10 http://aperiodic.net/phil/scala/s-99/ | |
* | |
* Run-length encoding of a list. | |
* | |
* Use the result of problem P09 to implement the so-called run-length encoding | |
* data compression method. Consecutive duplicates of elements are encoded as | |
* tuples (N, E) where N is the number of duplicates of the element E. |
OlderNewer