Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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: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: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:2006228
Created March 9, 2012 11:51
Scalaz Logger monad example
type StringLogger[A] = Logger[String, A]
def startWith(i: Int): StringLogger[Int] = i.logger :+-> "Started with " + i
def thenAdd(j: Int)(i: Int): StringLogger[Int] = (i + j).logger :+-> "Added " + j
def thenMultBy(j: Int)(i: Int): StringLogger[Int] = (i * j).logger :+-> "Multiplied by " + j
val loggedResult = startWith(1) >>= thenAdd(4) >>= thenMultBy(5)
@bmjames
bmjames / gist:1557596
Created January 3, 2012 23:39
totally non-complex way to list files in a directory
val openFile: String => IO[File]
= path => io { new File(path) }
val fileName: File => String
= _.toString
val putStrLn: String => IO[Unit]
= s => io { println(s) }
val listFiles: File => IO[Seq[File]]
#!/usr/bin/env bash
if ([ -z "$1" ] || [ -z "$2" ]); then
echo "usage: $0 <alias> <branch>"
exit
fi
CMD="git symbolic-ref refs/heads/$1 refs/heads/$2"
echo $CMD
$CMD
@bmjames
bmjames / gevent_zmq_pubsub.py
Created April 17, 2011 23:45
Async ZeroMQ example using gevent_zeromq
import gevent
from gevent_zeromq import zmq
context = zmq.Context()
def sub():
subscriber = context.socket(zmq.SUB)
subscriber.connect('tcp://localhost:5561')
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True: