Port of memoization example from FunWithTypeFuns.
scala> memo.MemoExamples.b2i(true)
b=true
res1: Int = 1
scala> memo.MemoExamples.b2i(false)
b=false
res2: Int = 0
trait Category[Cat[-_,+_]] { | |
def id[A]: Cat[A, A] | |
def andThen[A, B, C](f: Cat[A, B], g: Cat[B, C]): Cat[A, C] | |
} | |
object Category { | |
implicit object CategoryFunction1 extends Category[Function1] { | |
def id[A]: A => A = a => a | |
def andThen[A, B, C](f: A => B, g: B => C): A => C = f andThen g | |
} |
trait Profunctor[F[_, _, _, _], P[_, _]] { | |
def lmap[A, B, C](f: F[C, A, B, B])(p: P[A, B]): P[C, B] | |
def rmap[A, B, C](g: F[A, A, B, C])(p: P[A, B]): P[A, C] | |
def dimap[A, B, C, D](h: F[C, A, B, D])(p: P[A, B]): P[C, D] | |
} | |
object Profunctor { | |
type FilterLike[A, B, C, D] = Filter[A, D, B, C] | |
private trait FilterPartial[A, B] { type Apply[C, D] = Filter[C, D, A, B] } |
{-# LANGUAGE GADTs, InstanceSigs #-} | |
newtype Service a b = Service { unService :: a -> IO b } | |
newtype SF a b = SF { unSF :: Bool -> IO (Service a b) } | |
newtype Filter a b c d = Filter { unFilter :: c -> Service a b -> IO d } | |
class Profunctor p where | |
dimap :: Filter a b c d -> p a b -> p c d |
// Correct by construction. | |
// Adapted from https://gist.github.com/gelisam/9845116. | |
trait Opened | |
trait Closed | |
sealed trait Transition[S,T,A] | |
case object Open extends Transition[Closed,Opened,Unit] | |
case object Read extends Transition[Opened,Opened,String] | |
case object Close extends Transition[Opened,Closed,Unit] |
Port of memoization example from FunWithTypeFuns.
scala> memo.MemoExamples.b2i(true)
b=true
res1: Int = 1
scala> memo.MemoExamples.b2i(false)
b=false
res2: Int = 0
Regular Option
sealed trait Option[+A]
case class Some[+A](a: A) extends Option[A]
case object None extends Option[Nothing]
implicit def optionMonad[A](o: Option[A]) =
new MonadOps[A] {
def flatMap[B](f: A => Option[B]) = o match {
scala> class Bar(val a: Object) extends AnyVal | |
defined class Bar | |
scala> val x = new Bar(new Object) | |
x: Bar = Bar@5d0b5bcb | |
scala> :javap -v x | |
Size 599 bytes | |
MD5 checksum cc127130545aa10c64d151abbffcae83 | |
Compiled from "<console>" |
sealed trait Value | |
sealed trait Prim[A] extends Value { | |
def value: A | |
} | |
case class StringPrim(value: String) extends Prim[String] | |
val x: Value = StringPrim("hi") | |
def get[A](v: Value): Option[A] = v match { | |
case p: Prim[A] => Some(p.value) | |
case _ => None |