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 {
package com.twitter.concurrent | |
import com.twitter.util.{Await, Future} | |
/** | |
* Value class to wrap the Cons cell. | |
*/ | |
protected case class Cons[A](tuple: (A, AsyncStream[A])) extends AnyVal { | |
def head: A = tuple._1 | |
def tail: AsyncStream[A] = tuple._2 |
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 ~>[F[_], G[_]] { | |
def apply[A](f: F[A]): G[A] | |
} | |
trait Monad[M[_]] { | |
def pure[A](a: A): M[A] | |
def flatMap[A, B](a: M[A])(f: A => M[B]): M[B] | |
} | |
object Monad { |
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 {
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
// 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] |
{-# 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 |
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] } |
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 | |
} |
package com.twitter.finagle.util | |
//import language.{ higherKinds, implicitConversions } | |
trait Unapply[TC[_[+_]], FA] { | |
type F[+_] | |
type A | |
def TC: TC[F] | |
def apply(fa: FA): F[A] | |
} |