Skip to content

Instantly share code, notes, and snippets.

@bmjames
bmjames / gist:2293263
Created April 3, 2012 16:12
Partial lenses (Scalaz 7)
case class Person(pants: Option[Pants])
case class Pants(pocket: Option[Pocket])
case class Pocket(cash: Option[Cash])
case class Cash(value: String)
val pants: Person @-@ Option[Pants] =
lensG(_.pants, p => ps => p.copy(pants = ps))
val pocket: Pants @-@ Option[Pocket] =
lensG(_.pocket, ps => p => ps.copy(pocket = p))
val cash: Pocket @-@ Option[Cash] =
@bmjames
bmjames / gist:2429721
Created April 20, 2012 15:38
Quick'n'dirty ivy2-to-m2 shovel
#!/usr/bin/env bash
# Shovel stuff from ivy cache into m2 cache
if ([ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]); then
echo "usage: $0 <groupId> <artifactId> <version>"
exit
fi
cd ~/.ivy2/local/$1/$2/$3
@bmjames
bmjames / gist:2875449
Created June 5, 2012 14:48 — forked from debasishg/gist:762736
wordcount example in scalaz using State monad
import scalaz._
import Scalaz._
def charLineCount[T[_]:Traverse](t: T[Char]) =
t.traverse[({type λ[x] = State[(Int, Int),x]})#λ, (Int, Int)](a =>
state((counts: (Int, Int)) =>
((counts._1 + 1, counts._2 + (if (a == '\n') 1 else 0)), (counts._1, counts._2)))) ! (1,1)
println(charLineCount("the cat in the hat\n sat on the mat\n".toList).last) // (35, 2)
@bmjames
bmjames / gist:3132781
Created July 17, 2012 23:16
Enumerateaser
// Import ALL the things
import scalaz._, Scalaz._, iteratee._, Iteratee._, effect._
// An enumerator based on an iterator
val input = enumIterator[String, IO](Iterator("a", "bb", "ccc", "dddd"))
// An enumeratee which maps strings to their length
def strLen[F[_]:Monad] = EnumerateeT.map[String, Int, F](_.length)
// An iteratee which prints to System.out
@bmjames
bmjames / gist:3136643
Created July 18, 2012 14:50
An enumeratee which takes every Nth element and discards the rest
def takeEveryNth[E, F[_]:Monad](n: Int): EnumerateeT[E, E, F] =
new EnumerateeT[E, E, F] {
def apply[A] = {
def loop(m: Int) = step(m) andThen cont[E, F, StepT[E, F, A]]
def step(m: Int): (Input[E] => IterateeT[E, F, A]) => (Input[E] => IterateeT[E, F, StepT[E, F, A]]) = {
k => in => in(
empty = cont(step(m)(k)),
eof = done(scont(k), in),
el = e => if (m == n) k(in) >>== doneOr(loop(1))
else cont(step(m+1)(k))
@bmjames
bmjames / gist:3342080
Created August 13, 2012 15:48
Simple memoization using the State monad
def memoized[A, B](f: A => B): A => State[Map[A, B], B] =
a => State { m =>
m get a match {
case Some(b) => (b, m)
case None =>
val b = f(a)
(b, m.updated(a, b))
}
}
@bmjames
bmjames / gist:3378799
Created August 17, 2012 13:45
filterMN
import scalaz._, syntax.monad._
final def filterMN[A, M[_]:Monad, N[_]:Monad](as: List[A])(p: A => N[M[Boolean]]): N[M[List[A]]] =
as match {
case Nil => Monad[N].point(Monad[M].point(Nil))
case h :: t =>
for {
mb <- p(h)
mg <- filterMN(t)(p)
} yield {
@bmjames
bmjames / gist:3789162
Created September 26, 2012 16:55
Balanced parentheses using StateT
object PragmaticParens {
import scalaz._, Scalaz._, typelevel._
private def f(c: Char): StateT[Option, Nat, Unit] =
StateT(nat => (c match {
case '(' => Some(nat.succ)
case ')' => nat.pred
case _ => Some(nat)
}) map (_ -> ()))
@bmjames
bmjames / gist:4585409
Last active December 11, 2015 10:18
foldMap
import scalaz.Scalaz._
/** The problem: given the following database query results, associate each parentId to
* a List of filenames.
*/
case class Row(parentId: Int, file: String)
val queryResult = Stream(Row(123, "foo.jpg"), Row(123, "bar.jpg"), Row(234, "foo.png"))
/** Using foldLeft is verbose because we have to specify:
* - how to create an empty Map to begin with
@bmjames
bmjames / gist:5319410
Last active December 15, 2015 20:29
Stream implementation
object Chap5 extends App {
import Stream._
def ones: Stream[Int] = cons(1, ones)
def lin: Stream[Int] = cons(1, lin.map(_ + 1))
println(ones.flatMap(_ => empty[Int])) // Stack overflow!
}
sealed trait Stream[A] {