This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |