Skip to content

Instantly share code, notes, and snippets.

@nuttycom
nuttycom / gist:1232985
Created September 21, 2011 18:58
A potential higher-kinded HttpService for BlueEyes
import scalaz._
import scalaz.Scalaz._
trait Future[A]
object Future {
def async[A](a: A): Future[A] = error("todo")
}
sealed trait NotServed
@nuttycom
nuttycom / gist:1233639
Created September 21, 2011 23:46
Semigroup and monoid actions
/** Semigroup Action */
trait SAct[A, -B] { outer =>
def append(a: A, b: => B): A
def xappend(b: B, a: => A): A = append(a, b)
}
trait SActs {
implicit def s2sact[A](implicit semigroup: Semigroup[A]): SAct[A, A] = new SAct[A, A] {
override def append(a1: A, a2: => A): A = semigroup.append(a1, a2)
}
@nuttycom
nuttycom / BindSemigroup.scala
Created December 18, 2011 23:12 — forked from jdegoes/BindSemigroup.scala
BindSemigroup
implicit def BindSemigroup[M[_], A](implicit bind: Bind[M], s: Semigroup[A]): Semigroup[M[A]] = new Semigroup[M[A]] {
def append(v1: M[A], v2: => M[A]): M[A] = bind.bind(v1)((a: A) => v2.map(s.append(a, _)))
}
@nuttycom
nuttycom / strategy.java
Created March 17, 2012 19:24
Typeclasses and the Strategy pattern
// first, the Strategy pattern, in Java:
interface AdditionStrategy {
public int add(int a1, int a2);
}
// A simple polymorphic version - abstract over the type being added
interface PolyAdditionStrategy<A> {
public A add(A a1, A a2);
}
@nuttycom
nuttycom / gist:3049112
Created July 4, 2012 19:21
Wanted: List[(A, B)] => (List[A], List[B])
scala> import scalaz._
import scalaz._
scala> import scalaz.std.list._
import scalaz.std.list._
scala> import scalaz.std.tuple._
import scalaz.std.tuple._
scala> import scalaz.syntax.bitraverse._
@nuttycom
nuttycom / gist:3177071
Created July 25, 2012 16:25
Identity Monad
trait Monad[M[_]] {
def pure[A](a: A): M[A]
def flatMap[A, B](f: A => M[B]): M[A] => M[B]
def map[A, B](f: A => B): M[A] => M[B] = flatMap(a => pure(f(a)))
}
object Id {
type Id[X] = X
implicit val M: Monad[Id] = sys.error("todo")
@nuttycom
nuttycom / gist:3855346
Created October 8, 2012 22:20
Simple switch/dispatch microbench
import scala.annotation.tailrec
sealed abstract class A {
def i: Int
}
object A {
@inline final def tpIdx(a: A) = a match {
case SA(_) => 0
case BA(_) => 1
package test
import collection.mutable.ListBuffer
/**
* @author Matt Hicks <[email protected]>
*/
trait Element
trait Parentable extends Element {
@nuttycom
nuttycom / gist:4507997
Created January 11, 2013 04:41
This is a bit of a curiosity exploring the space of the expression problem by using a fold to define an algebra over multiple subtrees (and multiple levels) of an inheritance hierarchy. I'm not sure if it's good for anything yet, but it does allow for some interesting patterns using the combination of polymorphic dispatch and a fold.
trait A {
// define an algebra over the known space of subtypes
def fold[X](af: A => X, bf: B => X, cf: C => X): X
}
trait B extends A {
def fold[X](af: A => X, bf: B => X, cf: C => X): X = bf(this)
}
@nuttycom
nuttycom / Functorial
Last active December 18, 2015 08:09
A hypothesis as to what functors, monads, etc. might look like if the value type were not universally quantified, but instead could be restricted by requirement for other typeclass instances. Universal quantification could still be achieved, of course; hence this provides a more flexible generalization of our familiar typeclasses. This grew out …
trait Functorial[F[_], B] {
def map[A](m: F[A])(f: A => B): F[B]
}
trait Monadic[M[_], B] extends Functorial[F, B] {
def point(b: B): M[B]
def flatMap[A](m: M[A])(f: A => M[B]): M[B]
}
trait Traversial[F[_], B] {