Skip to content

Instantly share code, notes, and snippets.

@nuttycom
nuttycom / ListAlgebra.hs
Created November 6, 2014 04:11
Haskell list catamorphism
{-# LANGUAGE RankNTypes #-}
module ListAlgebra
( ListAlgebra(..)
) where
newtype ListAlgebra a = ListAlgebra (forall b. b -> (a -> b -> b) -> b)
nil :: ListAlgebra a
nil = ListAlgebra const
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) => ... }
@nuttycom
nuttycom / gist:dc442de9faec92fbf5af
Created October 18, 2014 21:13
Cabal sandbox complaining about reinstalls?
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
@nuttycom
nuttycom / .ctags
Created October 25, 2013 20:41
Scala ctags configuration.
--langdef=scala
--langmap=scala:.scala
--regex-scala=/^.*trait[ \t]+([a-zA-Z0-9_]+)/\1/t,traits/
--regex-scala=/^.*class[ \t]+([a-zA-Z0-9_]+)/\1/c,classes/
--regex-scala=/^.*object[ \t]+([a-zA-Z0-9_]+)/\1/o,objects/
--regex-scala=/^.*case class[ \t]+([a-zA-Z0-9_]+)/\1/c,case classes/
--regex-scala=/^.*case object[ \t]+([a-zA-Z0-9_]+)/\1/o,case objects/
--regex-scala=/^.*type[ \t]+([a-zA-Z0-9_]+)/\1/T,types/
--regex-scala=/^.*def[ \t]+([a-zA-Z0-9_\?]+)/\1/m,methods/
--regex-scala=/^.*val[ \t]+([a-zA-Z0-9_]+)/\1/C,constants/
@nuttycom
nuttycom / gist:6800642
Created October 2, 2013 21:15
Monad for the right side of Either.
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")
}
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
}
}
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 } =
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
@nuttycom
nuttycom / Functorial
Last active December 18, 2015 08:09
A hypothesis as to what functors, monads, etc. might look like if the value type were not universally quantified, but instead could be restricted by requirement for other typeclass instances. Universal quantification could still be achieved, of course; hence this provides a more flexible generalization of our familiar typeclasses. This grew out …
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] {
@nuttycom
nuttycom / gist:4507997
Created January 11, 2013 04:41
This is a bit of a curiosity exploring the space of the expression problem by using a fold to define an algebra over multiple subtrees (and multiple levels) of an inheritance hierarchy. I'm not sure if it's good for anything yet, but it does allow for some interesting patterns using the combination of polymorphic dispatch and a fold.
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)
}