This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE RankNTypes #-} | |
module ListAlgebra | |
( ListAlgebra(..) | |
) where | |
newtype ListAlgebra a = ListAlgebra (forall b. b -> (a -> b -> b) -> b) | |
nil :: ListAlgebra a | |
nil = ListAlgebra const |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Foo { | |
def unapply(a: ASup): Option[B] = | |
a match { | |
case a0: A => a0.b match { case b: B => Some(b); case _ => None } | |
case _ => None | |
} | |
} | |
ax collect { case Foo(b) => ... } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cabal install --only-dependencies | grep reinstall 1 ↵ | |
unix-2.7.0.1 (reinstall) changes: bytestring-0.10.4.0 -> 0.10.2.0 | |
directory-1.2.1.0 (reinstall) | |
process-1.2.0.0 (reinstall) | |
cabal: The following packages are likely to be broken by the reinstalls: | |
haskell98-2.0.0.3 | |
ghc-7.8.3 | |
Cabal-1.18.1.4 | |
bin-package-db-0.0.0.0 | |
haskeline-0.7.1.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Monad[M[_]] { | |
def pure[A](a: A): M[A] | |
def flatMap[A, B](f: A => M[B]): M[A] => M[B] | |
def map[A, B](f: A => B): M[A] => M[B] = flatMap(a => pure(f(a))) | |
} | |
class EitherRightMonad[L] extends Monad[({type M[R] = Either[L, R]})#M] { | |
def pure[A](a: A): Either[L, A] = error("todo") | |
def flatMap[A, B](f: A => Either[L, B]): Either[L, A] => Either[L, B] = error("todo") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class MaxBy[A](t: (A, A)) { | |
def maxBy[B: Order](f: A => B) = | |
(Order[A].order _).tupled(t umap f) match { | |
case GT | EQ => t._1 | |
case LT => t._2 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Functor { | |
type M <: { type T } | |
def fmap[A, B](fa: M { type T = A })(f: A => B): M { type T = B } | |
} | |
implicit class EitherRightFunctor extends Functor { self => | |
type L | |
type M = Either { type A = self.L ; type T = B } //doesn't this specify a subtype of Either, rather than Either itself? | |
def fmap[A0, B0](fa: M { type A = self.L ; type B = A0 })(f: A0 => B0): Either { type A = self.L ; type B = B0 } = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def foo[T, Coll[T] <: Seq[T]] = ??? | |
warning: there were 1 feature warning(s); re-run with -feature for details | |
foo: [T, Coll[T] <: Seq[T]]=> Nothing | |
scala> foo[Int, List[Int]] | |
<console>:9: error: List[Int] takes no type parameters, expected: one | |
foo[Int, List[Int]] | |
^ | |
scala> type LA[A] = List[Int] | |
defined type alias LA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Functorial[F[_], B] { | |
def map[A](m: F[A])(f: A => B): F[B] | |
} | |
trait Monadic[M[_], B] extends Functorial[F, B] { | |
def point(b: B): M[B] | |
def flatMap[A](m: M[A])(f: A => M[B]): M[B] | |
} | |
trait Traversial[F[_], B] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait A { | |
// define an algebra over the known space of subtypes | |
def fold[X](af: A => X, bf: B => X, cf: C => X): X | |
} | |
trait B extends A { | |
def fold[X](af: A => X, bf: B => X, cf: C => X): X = bf(this) | |
} |