Skip to content

Instantly share code, notes, and snippets.

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] =
import language.{ higherKinds, implicitConversions }
trait Unapply[TC[_[+_]], FA] {
type F[+_]
type A
def TC: TC[F]
def apply(fa: FA): F[A]
}
object Unapply {
@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
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 / 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 {
@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 / Inject.scala
Created May 28, 2013 18:16
"dependency injection" monad (for the lulz)
case class Inject[-E, +A](inject: E => A) {
// covariant functor and monad
def map[B](f: A => B): Inject[E, B] =
Inject { e => f(inject(e)) }
def flatMap[ε <: E, B](f: A => Inject[ε, B]): Inject[ε, B] =
Inject { e => f(inject(e)).inject(e) }
// to satisfy for-comprehension desugaring in scala < 2.10
def filter(f: A => Boolean): Inject[E, A] =
@mergeconflict
mergeconflict / FantasyLand.java
Last active December 16, 2015 04:28
Java 8 fantasy land?
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/*
* Java 8 fantasy land spec:
*
* A monad is a parameterized data type `M` with the following constraints
* - An instance `M<A>` must have a method: `public <B> M<B> bind(Function<A, M<B>> f)`
* - `M` must have a static method: `public static <A> M<A> point(A value)`
@mergeconflict
mergeconflict / Bug.java
Created April 13, 2013 04:12
Java 8 lulz
import java.util.function.Function;
public interface Bug {
static void bug() {
bug(a -> a);
}
static void bug(Function<?, ?> f) {}
}
@mergeconflict
mergeconflict / BlockingQueue.java
Last active December 15, 2015 21:58
Queue based on Hinze-Paterson 2-3 finger trees (http://www.soi.city.ac.uk/~ross/papers/FingerTree.pdf), BlockingQueue based on AtomicReference and a counting Semaphore.
package mergeconflict.collection.concurrent;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicReference;
import mergeconflict.collection.immutable.Queue;
public class BlockingQueue<E> {
private final Semaphore s = new Semaphore(0);
private final AtomicReference<Queue<E>> q = new AtomicReference<Queue<E>>(Queue.<E> empty());