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
#!/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
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
// 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
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
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
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 { |
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
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 (_ -> ())) |
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.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 |
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
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] { |