This file contains 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
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] = |
This file contains 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
import language.{ higherKinds, implicitConversions } | |
trait Unapply[TC[_[+_]], FA] { | |
type F[+_] | |
type A | |
def TC: TC[F] | |
def apply(fa: FA): F[A] | |
} | |
object Unapply { |
This file contains 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
// 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 |
This file contains 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
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 ++ ")" |
This file contains 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
package sandbox | |
import akka.actor.{ Actor, ActorRef, ActorSystem, Props } | |
class Println extends Actor { | |
def receive = { | |
case message => println(message) | |
} | |
} | |
object Println { |
This file contains 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
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]) = { |
This file contains 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
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] = |
This file contains 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
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)` |
This file contains 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
import java.util.function.Function; | |
public interface Bug { | |
static void bug() { | |
bug(a -> a); | |
} | |
static void bug(Function<?, ?> f) {} | |
} |
This file contains 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
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()); |
NewerOlder