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
package com.codecommit.misc
import scalaz._
import scalaz.concurrent.Task
import scalaz.iteratee._
import scalaz.stream._
object conversion {
// TODO generalize to EmitterT
import annotation.unchecked.uncheckedVariance
import reflect.runtime.universe.TypeTag
package object stuff {
// required for the better ??? operator
type MyTypeTag[+A] = TypeTag[A @uncheckedVariance]
def ???[A](implicit tag: MyTypeTag[A]): A =
throw new NotImplementedError(s"unimplemented value of type ${tag.tpe}")
}
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
*/
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:1168340
Created August 24, 2011 15:42
Encoding of List using rank-3 types (Runar's code from http://paste.pocoo.org/show/463632/)
import scalaz._
import Scalaz._
type List[A] = Forall[({type f[B] = ((A, B) => B, B) => B})#f]
val nil: Forall[List] = new Forall[List] {
def apply[A] = new List[A] {
def apply[B] = (c, n) => n
}
}
@paulp
paulp / .gitconfig
Created December 1, 2011 00:12
my gitconfig
[user]
name = Paul Phillips
email = paulp@improving.org
[github]
user = $GITHUB_USER
token = $GITHUB_TOKEN
[clean]
requireForce = false
[grep]
lineNumber = true
@retronym
retronym / unimplicitly.scala
Created April 20, 2012 20:59
unimplicitly
scala> import scala.language.experimental.macros
import scala.language.experimental.macros
scala> def unimplicitlyImpl[A: c.TypeTag](c: reflect.makro.Context) = {
| val A = implicitly[c.TypeTag[A]].tpe
| val i = c.inferImplicitValue(A, silent = true)
| if (i == c.mirror.EmptyTree) c.reify(())
| else sys.error("unexpected implicit of type %s: %s".format(A, i))
| }
unimplicitlyImpl: [A](c: scala.reflect.makro.Context)(implicit evidence$1: c.TypeTag[A])c.mirror.Expr[Unit]