Skip to content

Instantly share code, notes, and snippets.

import java.util.UUID
object Test {
implicit def columnMap2Map[A, B](map: Map[Array[Byte], Array[Byte]]) = new MapConverter(map)
class MapConverter(map: Map[Array[Byte], Array[Byte]]) {
def to[A, B](implicit f: Array[Byte] => A, g: Array[Byte] => B): Map[A, B] = {
map.foldLeft(Map[A, B]()) { case(map, (k, v)) =>
val tuple = (k: A, v: B)
map + tuple
@nuttycom
nuttycom / gist:714663
Created November 24, 2010 23:58
What Iterator.iterate should have been.
object Bah {
def iterate[T](t: T)(f: T => Option[T]): Iterator[T] = new Iterator {
var next = Some(t)
override def hasNext = next.isDefined
override def next = {
val result = next.get
next = f(result)
result
}
}
@nuttycom
nuttycom / BarTrait.scala
Created June 7, 2011 19:42
Sample configuration for recompilation failure
// in src/main/scala/foo/bar/BarTrait.scala
package foo.bar
trait BarTrait
@nuttycom
nuttycom / MapImplicits.scala
Created June 8, 2011 21:42
Map implicits for scalaz
implicit def MapMonoid[K, V](implicit valueSemigroup: Semigroup[V]): Monoid[Map[K, V]] = new Monoid[Map[K, V]] {
override val zero = Map.empty[K, V]
override def append(m1: Map[K, V], m2: => Map[K, V]) = {
val (from, to, semigroup) = {
if (m1.size > m2.size) (m2, m1, (v1: V, v2: V) => valueSemigroup.append(v1, v2))
else (m1, m2, (v1: V, v2: V) => valueSemigroup.append(v2, v1))
}
from.foldLeft(to) {
case (to, (k, v)) => to + (k -> to.get(k).map(semigroup(_, v)).getOrElse(v))
@nuttycom
nuttycom / MapImplicits.scala
Created June 8, 2011 21:42
Map implicits for scalaz
implicit def MapMonoid[K, V](implicit valueSemigroup: Semigroup[V]): Monoid[Map[K, V]] = new Monoid[Map[K, V]] {
override val zero = Map.empty[K, V]
override def append(m1: Map[K, V], m2: => Map[K, V]) = {
val (from, to, semigroup) = {
if (m1.size > m2.size) (m2, m1, (v1: V, v2: V) => valueSemigroup.append(v1, v2))
else (m1, m2, (v1: V, v2: V) => valueSemigroup.append(v2, v1))
}
from.foldLeft(to) {
case (to, (k, v)) => to + (k -> to.get(k).map(semigroup(_, v)).getOrElse(v))
@nuttycom
nuttycom / gist:1037030
Created June 21, 2011 01:21
Reader 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)))
}
class Reader[I] extends Monad[({type M[O] = Function1[I, O]})#M] {
def pure[A](a: A): Function1[I, A] = error("todo")
def flatMap[A, B](f: A => Function1[I, B]): Function1[I, A] => Function1[I, B] = error("todo")
}
@nuttycom
nuttycom / gist:1072932
Created July 8, 2011 21:58
Thrush combinator using partial function => Option
implicit def pf[A](a: A): PF[A] = new PF(a)
class PF[A](a: A) {
def option[B](pf: PartialFunction[A, B]): Option[B] = pf.lift.apply(a)
}
@nuttycom
nuttycom / gist:1088005
Created July 17, 2011 20:09
Kleislis & Monad Transformsers
(08:14:03 PM) nuttycom: dobblego: if you're about, there is something I'd like to learn from you. :)
(08:14:12 PM) dobblego: I have 15 minutes
(08:14:17 PM) nuttycom: I'd like to learn the meaning of "kleisli"
(08:14:27 PM) dobblego: it's the monad transformer for Function1
(08:14:35 PM) dobblego: A => F[B]
(08:14:59 PM) dobblego: it gives some useful properties
(08:15:14 PM) dobblego: if F is a monad, then A => F[?] is a monad
(08:15:31 PM) copumpkin: another view is that every monad gives rise to a category
(08:15:42 PM) copumpkin: where you have identity and composition like you do with regular functions
(08:16:09 PM) copumpkin: except composition is (A => M[B], B => M[C]): A => M[C]
@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/
@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)