Skip to content

Instantly share code, notes, and snippets.

View retronym's full-sized avatar

Jason Zaugg retronym

View GitHub Profile
@retronym
retronym / low-priority-implicits.scala
Created November 7, 2009 13:00
Scala 2.8 implicit prioritisation, as discussed in: http://www.scala-lang.org/sid/7
object Low {
def low = "object Low"
def shoot = "missed!"
}
object High {
def high = "object High"
def shoot = "bulls eye!"
}
@retronym
retronym / this-type.scala
Created November 7, 2009 14:50
The special type this.type
trait T {
def me: this.type = this
}
class W {
def woohoo: String = "woohoo"
}
println((new W with T).me.woohoo)
@retronym
retronym / option-flatten.scala
Created November 7, 2009 22:56
Use of generalised type constraints to implement Option.flatten
sealed trait Option[+A] {
def isDefined: Boolean
def flatten[B](implicit ev: A <:< Option[B]): Option[B]
}
case object None extends Option[Nothing] {
def isDefined = false
def flatten[B](implicit ev: Nothing <:< Option[B]): Option[B] = None
}
@retronym
retronym / generalised-type-constraints.scala
Created November 8, 2009 08:02
Demo of generalised type constraints in Scala 2.8
// scala 2.7 simple type constraint. This can only constrain a type parameter of this function.
// Below, in ListW.sumint28, we can't use this approach because we want to constrain T,
// a type param of the enclosing trait.
def sumint27A[T <: Int](l: List[T]) : Int = l.reduceLeft((a: Int, b: Int) => a + b)
trait IntLike[X] extends (X => Int)
object IntLike {
implicit val intIntLike: IntLike[Int] = new IntLike[Int] { def apply(x: Int) = identity(x) }
}
@retronym
retronym / composable-builder-with-this-type.scala
Created November 8, 2009 08:03
Composable Builder Pattern using this.type in Scala
trait Buildable[T] {
def build: T
}
trait HeadBuilder extends Buildable[String] {
var eyeColor = "brown"
var hairColor = "red"
def withEyeColor(color: String): this.type = {
eyeColor = color
// Named <::< to clearly distinguish from Predef.<:<
sealed abstract class <::<[-From, +To] extends (From => To)
object <::< {
implicit def conforms[A]: A <::< A = new (A <::< A) {def apply(x: A) = x}
}
sealed abstract class =:=[From, To] extends (From => To)
object =:= {
implicit def tpEquals[A]: A =:= A = new (A =:= A) {def apply(x: A) = x}
}
@retronym
retronym / type-relations.scala
Created November 25, 2009 13:01
Type Relations in scala
/**
* Type Equivalence Bound
*/
sealed abstract class =:=[From, To] extends (From => To) {
override def toString = "=:=[From, To]"
}
object =:= {
implicit def typeEquals[A]: A =:= A = new (A =:= A) {
def apply(x: A) = x
class Scratch
trait PartialApply1Of2[T[_, _], A] {
type Apply[B] = T[A, B]
type Flip[B] = T[B, A]
}
trait Cofunctor[F[_]] {
def comap[A, B](r: F[A], f: B => A): F[B]
}
@retronym
retronym / implicit-serach-failure.scala
Created December 9, 2009 06:39
Trying to update scalaz Pure typeclass to use GenericCompanion.apply...
class Scratch
import collection.generic.GenericCompanion
trait HasGenericCompanion[S[X] <: Traversable[X]] {
def companion: GenericCompanion[S]
}
object HasGenericCompanion {
lazy implicit val StreamHasCompanion: HasGenericCompanion[Stream] = new HasGenericCompanion[Stream] {
import java.util.ArrayList
object Scratch
trait Empty[+E[_]] {
def empty[A]: E[A]
}
object Emptys {
def <∅>[E[_], A](implicit e: Empty[E]): E[A] = e.empty