Skip to content

Instantly share code, notes, and snippets.

View derekjw's full-sized avatar

Derek Williams derekjw

  • FP Solutions Ltd
  • London, United Kingdom
View GitHub Profile
@arnolddevos
arnolddevos / gist:574873
Created September 11, 2010 05:36
Idea for generator and suspendable wrapper
import scala.util.continuations._
class Generator[A] extends Iterator[A] with (A => Unit @ suspendable) {
private var a: Option[A] = None
private var k: Option[Unit => Unit] = None
def next = {
val a0 = a.get
val k0 = k.get
a = None
import Responder._
import scalaz._
object ResponderDemo extends Application {
import Scalaz._
// --------------------------------------------------------------------------
// Uses Responder (a continuation monad) to compose asynchronous functions.
// --------------------------------------------------------------------------
@debasishg
debasishg / gist:762328
Created January 2, 2011 05:47
loop and label implementation of the Essence of the Iterator Pattern
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 =>
@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] {
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
// 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
@debasishg
debasishg / gist:924143
Created April 17, 2011 15:36
@runanorama's code snippet for using iteratee based JDBC results processing
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)
}
@oxbowlakes
oxbowlakes / 3nightclubs.scala
Created May 13, 2011 15:14
A Tale of 3 Nightclubs
/**
* 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._
@jsuereth
jsuereth / ExampleSocketServer.scala
Created May 14, 2011 02:14
IterateeNonBlockingEchoServer
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