Skip to content

Instantly share code, notes, and snippets.

/**
* 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._
/*
The Oracle Java enum Planets example using the Klang DIY Enum, modified to include ordering.
*/
object KlangEnumPlanets extends App {
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
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
package demo
import scalaz._
import scalaz.syntax._
import scalaz.effect.IO
import scalaz.effect.IO._
import scalaz.syntax.std.either._
object Demo {
type M[A] = EitherT[IO, Throwable, A]
import scalaz.effect.IO
import scalaz.{EitherT, Monad}
object Program extends ProgramInstances {
def apply[A](a: A): Program[A] = Program[A](EitherT.right[IO, Throwable, A](IO(a)))
}
trait ProgramInstances {
implicit object ProgramMonad extends Monad[Program] {
override def point[A](a: => A): Program[A] = Program[A](a)
// No case classes
object Consoles {
trait ConsoleAlg[F[_]] {
def readLine: F[Option[String]]
def printLine(line: String): F[Unit]
}
trait Console[+A] {
/** GADTs in Scala and their limitations */
/** Background: what is an algebraic data type (ADT) ?
* ADT: (possibly) recursive datatype with sums and products
* In scala - a trait with case classes (case class is product, subtyping is sum)
*/
/** Motivation: untyped embedded DSL doesn't prevent nonsensical expressions */
sealed trait Expr {
def apply(other: Expr) = Ap(this, other)
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x