Skip to content

Instantly share code, notes, and snippets.

View tonymorris's full-sized avatar

Tony Morris tonymorris

View GitHub Profile
// see https://github.com/gseitz/Lensed
case class CoState[A, B](put: A => B, pos: A)
object CoState {
type Store[A, B] = CoState[A, B]
}
// fused get/set
case class Lens[A, B](lens: A => CoState[B, A]) {
@tonymorris
tonymorris / ReaderWriterStateT.scala
Created April 11, 2012 16:59
Reader/Writer/State transformer in Scala
case class ReaderWriterStateT[R, W, S, F[_], A](
run: (R, S) => F[(W, A, S)]
) {
def map[B](f: A => B)(implicit F: Functor[F])
: ReaderWriterStateT[R, W, S, F, B] =
ReaderWriterStateT {
case (r, s) => F.map(run(r, s)) {
case (w, a, s) => (w, f(a), s)
}
}
@tonymorris
tonymorris / Unzip
Created April 13, 2012 05:16
Ugly -> nicer?
class Functor f => Unzip f where
unfzip ::
f (a, b)
-> (f a, f b)
unfzip3 ::
f (a, b, c)
-> (f a, f b, f c)
unfzip3 x =
let (a, bc) = unfzip (fmap (\(a', b', c') -> (a', (b', c'))) x)
@tonymorris
tonymorris / EitherFilter.scala
Created June 18, 2012 23:50
How to write filter on Either, if such a thing must exist
trait Semigroup[A] {
def op(a1: A, a2: A): A
}
trait Monoid[A] extends Semigroup[A] {
def id: A
}
object EitherFilter {
def filterR[A, B](e: Either[A, B])(p: B => Boolean)(implicit M: Monoid[B]): Either[A, B] =
@tonymorris
tonymorris / gist:3039030
Created July 3, 2012 10:50
How can I get this program to terminate? (non-strict implicit not permitted)
case class Swizzler[A](swizzle: String => A) {
def map[B](f: A => B): Swizzler[B] =
Swizzler(f compose swizzle)
def flatMap[B](f: A => Swizzler[B]): Swizzler[B] =
Swizzler(s => f(swizzle(s)).swizzle(s))
}
object Swizzler {
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
trait Extend[F[_]] extends Functor[F] {
// coflatmap
def extend[A, B](f: F[A] => B): F[A] => F[B]
}
trait Comonad[F[_]] extends Extend[F] {
@tonymorris
tonymorris / gist:3087504
Created July 11, 2012 02:09
Comonad/Monad
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
trait Extend[F[_]] extends Functor[F] {
// coflatmap
def extend[A, B](f: F[A] => B): F[A] => F[B]
}
trait Comonad[F[_]] extends Extend[F] {
@tonymorris
tonymorris / gist:3088073
Created July 11, 2012 04:42
Semigroup/Monoid/Semigroupoid/Category/Endo
class Semigroupoid cat where
(<.>) ::
cat a b
-> cat b c
-> cat a c
class Semigroupoid cat => Category cat where
identity ::
cat a a
case class Person(name: String, age: Int)
object Person
case class OtherPerson(name: String, age: Int)
object T {
def ap[A, B, C](f: (A, B) => C)(a: A, b: B): C =
f(a, b)
@tonymorris
tonymorris / ghc.sh
Created July 14, 2012 02:15
GHC 7.0.4 install for Linux (apt-get)
#!/bin/sh
set -e
# Base temporary work directory
TMP="/tmp"
# Glasgow Haskell Compiler version
GHCVERSION=7.0.4