Skip to content

Instantly share code, notes, and snippets.

@dholbrook
dholbrook / simpleservicelocator.scala
Created May 26, 2011 22:13
service locator demo
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 {
@dholbrook
dholbrook / SleepingBarber.scala
Created October 12, 2011 03:03
Sleeping Barber Problem, Akka actor implementation
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)
@dholbrook
dholbrook / SleepingBarberOutput.txt
Created October 12, 2011 03:10
output for sleeping barber demo program
[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
@dholbrook
dholbrook / S99P03.scala
Created October 19, 2011 17:45
S99 Problem 3
/*
* 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
@dholbrook
dholbrook / Palindrome.scala
Created December 15, 2011 17:26
S99 Problem 6
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)
@dholbrook
dholbrook / s99p8.scala
Created January 5, 2012 06:21
S99 Problem 8
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
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)
*
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)
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))
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.