Skip to content

Instantly share code, notes, and snippets.

@mergeconflict
mergeconflict / Writer.scala
Created June 25, 2013 16:21
boring writer monad example for @johnynek
package sandbox
trait Monoid[A] {
def empty: A
def append(lhs: A, rhs: A): A
}
case class Writer[W, A](a: A, w: W) {
def map[B](f: A => B): Writer[W, B] = Writer(f(a), w)
def flatMap[B](f: A => Writer[W, B])(implicit W: Monoid[W]) = {
@mergeconflict
mergeconflict / Pythagoras.scala
Last active December 19, 2015 06:29
Akka continuation-passing style?
package sandbox
import akka.actor.{ Actor, ActorRef, ActorSystem, Props }
class Println extends Actor {
def receive = {
case message => println(message)
}
}
object Println {
data Term = Var Int
| Abs Term
| App Term Term
deriving Eq
instance Show Term where
show (Var index) = show index
show (Abs body) = "λ." ++ show body
show (App lhs rhs) = "(" ++ show lhs ++ " " ++ show rhs ++ ")"
@mergeconflict
mergeconflict / gist:8361764
Created January 10, 2014 20:15
¯\_(ツ)_/¯
// this is really "category" but omg math words
trait Stackable[S[_, _]] {
def compose[A, B, C](lhs: S[A, B], rhs: S[B, C]): S[A, C]
def id[A]: S[A, A]
}
object Stackable {
// for example ...
implicit val function1stackable = new Stackable[Function1] {
def compose[A, B, C](lhs: A => B, rhs: B => C): A => C = lhs andThen rhs
import language.{ higherKinds, implicitConversions }
trait Unapply[TC[_[+_]], FA] {
type F[+_]
type A
def TC: TC[F]
def apply(fa: FA): F[A]
}
object Unapply {
sealed trait Validation[+E, +A] {
def map[B](f: A => B): Validation[E, B]
def flatMap[EE >: E, B](f: A => Validation[EE, B]): Validation[EE, B]
def <*>[EE >: E, AA, B](aa: Validation[EE, AA])(implicit valueToFunction: A <:< (AA => B)): Validation[EE, B]
}
object Validation {
case class Success[+A](value: A) extends Validation[Nothing, A] {
def map[B](f: A => B): Validation[Nothing, B] =