Skip to content

Instantly share code, notes, and snippets.

@mergeconflict
mergeconflict / FoldableSyntax.scala
Created January 6, 2012 18:51
category folding fail
trait CategoryFoldableV[F[_], C[_, _], A] extends FoldableV[F, C[A, A]] {
implicit def C: Category[C]
////
final def composeRight = sumr(Monoid.liftCategory)
}
trait ToFoldableV0 {
implicit def ToFoldableVUnapply[FA](v: FA)(implicit F0: Unapply[Foldable, FA]) =
new FoldableV[F0.M,F0.A] { def self = F0(v); implicit def F: Foldable[F0.M] = F0.TC }
}
@mergeconflict
mergeconflict / pimps.scala
Created March 13, 2012 01:55
typeclass fu
object Hoho {
trait Foo[A] {
def phooey(): Unit
}
implicit def intFoo(i: Int): Foo[Int] = new Foo[Int] {
def phooey(): Unit = println("int " + i.toString)
}
@mergeconflict
mergeconflict / all.scala
Created March 24, 2012 22:24
packaging and unpackaging multiple implicit conversions in an HList
object AllExamples extends App {
import shapeless._
final class All[L <: HList](val values: L) {
def apply[A](implicit selector: Selector[L, A]): A = values.select[A]
}
object All {
// package a value of type A, convertible to type B, into an HList containing just B
module Main (main) where
import Control.Monad.RWS
import Control.Monad.Writer
import Data.Binary.Builder
import qualified Data.ByteString.Lazy as Lazy
import Data.Hashable
import Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as Map
-- import Data.Map (Map)
@mergeconflict
mergeconflict / Main.java
Created October 20, 2012 21:02
hello asm
package com.mergeconflict.outclass;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.GETSTATIC;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.*;
import java.io.FileOutputStream;
import java.io.IOException;
@mergeconflict
mergeconflict / gist:4819632
Created February 13, 2013 01:32
divergence!
scala> implicitly[Bijection[String, Int]]
bijection.this.Bijection.identity is not a valid implicit value for com.twitter.bijection.Bijection[String,Int] because:
type mismatch;
found : com.twitter.bijection.Bijection[Any,Any]
required: com.twitter.bijection.Bijection[String,Int]
Note: Any >: String, but trait Bijection is invariant in type A.
You may wish to define A as -A instead. (SLS 4.5)
Note: Any >: Int, but trait Bijection is invariant in type B.
You may wish to define B as -B instead. (SLS 4.5)
bijection.this.Bijection.identity is not a valid implicit value for com.twitter.bijection.Bijection[Int,String] because:
@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());
@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 / 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 / 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] =