Skip to content

Instantly share code, notes, and snippets.

Jason Zaugg retronym

  • Lightbend
  • Brisbane, Australia
View GitHub Profile
@retronym
retronym / low-priority-implicits.scala
Created Nov 7, 2009
Scala 2.8 implicit prioritisation, as discussed in: http://www.scala-lang.org/sid/7
View low-priority-implicits.scala
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 Nov 7, 2009
The special type this.type
View this-type.scala
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 Nov 7, 2009
Use of generalised type constraints to implement Option.flatten
View option-flatten.scala
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 Nov 8, 2009
Demo of generalised type constraints in Scala 2.8
View generalised-type-constraints.scala
// 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 Nov 8, 2009
Composable Builder Pattern using this.type in Scala
View composable-builder-with-this-type.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
View 2.8-type-relations.scala
// 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 Nov 25, 2009
Type Relations in scala
View type-relations.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
View functor-cofunctor.scala
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 Dec 9, 2009
Trying to update scalaz Pure typeclass to use GenericCompanion.apply...
View implicit-serach-failure.scala
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] {
View no-common-type-error.scala
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
You can’t perform that action at this time.