Skip to content

Instantly share code, notes, and snippets.

package test
import collection.mutable.ListBuffer
/**
* @author Matt Hicks <[email protected]>
*/
trait Element
trait Parentable extends Element {
@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
@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: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 / 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 / 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 / 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 / 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:1104363
Created July 25, 2011 15:17
Pattern matching on path-dependent types
sealed trait A {
type X
case class B(x: X)
def b: B
}
case class J(x: String) extends A {
type X = String
override def b = B(x)
@nuttycom
nuttycom / gist:1093305
Created July 19, 2011 18:11
Scala ctags
--langdef=scala
--langmap=scala:.scala
--regex-scala=/^.*case object[ \t]+([a-zA-Z0-9_]+)/\1/o,case objects/
--regex-scala=/^.*object[ \t]+([a-zA-Z0-9_]+)/\1/o,objects/
--regex-scala=/^.*case class[ \t]+([a-zA-Z0-9_]+)/\1/c,case classes/
--regex-scala=/^.*class[ \t]+([a-zA-Z0-9_]+)/\1/c,classes/
--regex-scala=/^.*trait[ \t]+([a-zA-Z0-9_]+)/\1/t,traits/
--regex-scala=/^.*type[ \t]+([a-zA-Z0-9_]+)/\1/T,types/
--regex-scala=/^.*def[ \t]+([a-zA-Z0-9_\?]+)/\1/m,methods/
--regex-scala=/^.*val[ \t]+([a-zA-Z0-9_]+)/\1/C,constants/