Skip to content

Instantly share code, notes, and snippets.

View viktorklang's full-sized avatar

Viktor Klang (√) viktorklang

View GitHub Profile
abstract class some[X](val value : X) { type T = X }
abstract class someFactory[S <: some[_]](val P : (S#T) => boolean)
{
def apply(t : S#T) : Option[S] = if(P(t)) Some(fetch(t)) else None
def unapply(s : S) : Option[S#T] = if(s == null) None else Some(s.value)
def fetch(t : S#T) = mk(t)
//isValid = predicate/constraint
abstract class Type[T](val isValid : (T) => boolean)
{
//X is the reference type
final protected[Type] case class X[T] (val value : T) {
override def toString = Type.this.getClass.getSimpleName + "(" + value + ")"
}
//Type is the alias for X[T] that we expose to the outside world
type Type = X[T]
object Test {
trait Mapping {
def get[T](clazz : Class[T]) : T
}
trait Injector {
protected val mapping : Mapping
def inject[T](implicit manifest : scala.reflect.Manifest[T]) = mapping.get(manifest.erasure.asInstanceOf[Class[T]])
}
package dining.hakkers
//Akka adaptation of
//http://www.dalnefre.com/wp/2010/08/dining-philosophers-in-humus/
import se.scalablesolutions.akka.actor.{Scheduler, ActorRef, Actor}
import se.scalablesolutions.akka.actor.Actor._
import java.util.concurrent.TimeUnit
/*
package my.concurrent.multimap
import scala.reflect.Manifest
import java.util.concurrent.{ConcurrentSkipListSet, ConcurrentHashMap}
import java.util.{Set => JSet}
import scala.collection.JavaConversions._
import annotation.tailrec
class Index[K <: AnyRef,V <: AnyRef : Manifest] {
//Usage:
//override def pomPostProcess(node: Node): Node = mcPom(moduleConfigurations)(super.pomPostProcess(node))
trait McPom { self: DefaultProject =>
import scala.xml._
def mcPom(mcs: Set[ModuleConfiguration])(node: Node): Node = {
//make sure we have a trailing slash so we deduplicate URLs properly
def cleanUrl(url: String) = url match {
case null => ""
@viktorklang
viktorklang / UnnestedReceives.scala
Created January 26, 2011 17:16
Unnesting a Scala Actors nested receive example
/**
* I had a question directed at me, on how to encode the following scenario in Akka Actors,
* as for Scala Actors one would simply nest the receives.
*
* Recuirements are as follows:
* The first thing the actor needs to do, is to subscribe to a channel of events,
* Then it must replay (process) all "old" events
* Then it has to wait for a GoAhead signal to begin processing the new events
* It mustn't "miss" events that happen between catching up with the old events and getting the GoAhead signal
*/
@viktorklang
viktorklang / CQRS_ES_Actor.scala
Created February 1, 2011 15:05
CQRS and EventSourcing using Akka Actors
import scala.collection.mutable.ListBuffer
import akka.actor.{Actor,ActorRef}
import akka.actor.Actor._
import akka.routing.{ Listeners, Listen }
//Represents a domain event
trait Event
//A builder to create domain entities
trait EntityBuilder[Entity] {
@viktorklang
viktorklang / MiniSTM.scala
Created February 9, 2011 18:06
Optimistic concurrency using AtomicReference
class MiniSTM[T <: AnyRef](initialValue: T) {
private val value = new AtomicReference[T](initialValue)
def apply(fun: T => T) {
@tailrec def update(expect: T): Unit =
if ( !value.compareAndSet(expect, fun(expect)) ) update(value.get)
update(value.get)
}
@viktorklang
viktorklang / PostStart.scala
Created March 5, 2011 23:13
PostStart implementation for Akka
trait PostStart { actor: Actor =>
def postStart: Unit
override def preStart {
actor.become {
case "PostStart" => try { postStart } finally { actor.unbecome }
}
actor.self ! "PostStart"
}
}