This file contains hidden or 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
trait Enum { //DIY enum type | |
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia | |
type EnumVal <: Value //This is a type that needs to be found in the implementing class | |
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values | |
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal | |
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS} | |
val oldVec = get |
This file contains hidden or 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 scalaz.example.nio | |
import scalaz._ | |
import concurrent.Promise | |
import effects.IO | |
import nio.sockets._ | |
import nio.Channels._ | |
import Scalaz._ | |
import iteratees._ | |
import java.nio.channels.SocketChannel |
This file contains hidden or 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
/** | |
* Part Zero : 10:15 Saturday Night | |
* | |
* (In which we will see how to let the type system help you handle failure)... | |
* | |
* First let's define a domain. (All the following requires scala 2.9.x and scalaz 6.0) | |
*/ | |
import scalaz._ | |
import Scalaz._ |
This file contains hidden or 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
def enumResultSet[E,A](rs: ResultSet, iter: IterV[E, A], get: ResultSet => IO[E]): IO[IterV[E, A]] = { | |
def loop(i: IterV[E, A]): IO[IterV[E, A]] = | |
i.fold(done = (_, _) => i.pure[IO], | |
cont = k => next(rs) >>= (hasMore => | |
if (!hasMore) i.pure[IO] | |
else get(rs) >>= (t => loop(k(El(t)))))) | |
loop(iter) | |
} |
This file contains hidden or 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
// See http://lampsvn.epfl.ch/trac/scala/ticket/967. | |
// | |
// Gilles' fix causes the definition of Nat to be rejected with the error | |
// "Parameter type in structural refinement may not refer to a type member | |
// of that refinement". However we can work around the problem by | |
// quantifying out the problematic parameter type and reinstating it via | |
// a generalized type constraint. | |
type Num = { | |
type Rep |
This file contains hidden or 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
case class ElectricCar(b: Battery) { def batteryLevel = b.filledPercentage } | |
case class GasolineCar(g: GasTank) { def gasLevel = g.filledPercentage } | |
case class Battery(filledPercentage: Int) { def fill: Battery = Battery(100) } | |
case class GasTank(filledPercentage: Int) { def fill: GasTank = GasTank(100) } | |
trait Fills[C] { | |
def fill(car: C): C |
This file contains hidden or 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.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] { |
This file contains hidden or 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
/** | |
* 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 | |
*/ |
This file contains hidden or 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 scalaz._ | |
import Scalaz._ | |
val l = List(10, 20, 30, 40) | |
// loop from The Essence of the Iterator Pattern | |
// accumulates elements effectfully, but modifies elements purely and independently of accumulation | |
def loop[T[_]:Traverse, A, B](f: A => B, t: T[A]) = | |
t.traverse[({type λ[x] = State[Int,x]})#λ, (B, Int)](a => |
This file contains hidden or 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 Responder._ | |
import scalaz._ | |
object ResponderDemo extends Application { | |
import Scalaz._ | |
// -------------------------------------------------------------------------- | |
// Uses Responder (a continuation monad) to compose asynchronous functions. | |
// -------------------------------------------------------------------------- |