Created
July 6, 2015 12:16
-
-
Save filippovitale/6cc45396ed917a2a8411 to your computer and use it in GitHub Desktop.
Send + More = Money – ScalaSyd – July 2015
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 org.specs2._ | |
import scalaz._, Scalaz._ | |
class SendMoreMoneySpec extends Specification { def is = s2""" | |
Using the List Monad | |
List(1, 2) $tl1 | |
List(1, 2) if a != b $tl2 | |
List(1, 2, 3) $tl3 | |
Map('a' -> a, 'b' -> b) $tl4 | |
s.eval(List(1, 2)) $ts1 | |
s.eval(List(1, 2, 3)) $tsl1 | |
g.eval(List(1, 2, 3)) $tg1 | |
""" | |
val l1 = for { | |
a <- List(1, 2) | |
b <- List(1, 2) | |
} yield (a, b) | |
val tl1 = l1 ==== List((1, 1), (1, 2), (2, 1), (2, 2)) | |
// --------------------------------------------------------------------------- | |
val l2 = for { | |
a <- List(1, 2) | |
b <- List(1, 2) if a != b | |
} yield (a, b) | |
val tl2 = l2 ==== List((1, 2), (2, 1)) | |
// --------------------------------------------------------------------------- | |
val l3 = for { | |
a <- List(1, 2, 3) | |
b <- List(1, 2, 3) | |
} yield (a, b) | |
val tl3 = l3 ==== List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)) | |
// --------------------------------------------------------------------------- | |
val l4 = for { | |
a <- List(1, 2) | |
b <- List(1, 2) if a != b | |
} yield Map('a' -> a, 'b' -> b) | |
val tl4 = l4 ==== List(Map('a' -> 1, 'b' -> 2), Map('a' -> 2, 'b' -> 1)) | |
// --------------------------------------------------------------------------- | |
val l5 = for { | |
a <- List(1, 2, 3) | |
b <- List(1, 2, 3) if a != b | |
c <- List(1, 2, 3) if a != c && b != c | |
} yield (a, b, c) | |
val tl5 = l5 ==== List((1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1)) | |
// --------------------------------------------------------------------------- | |
import scalaz.State | |
def selectS(xs: List[Int]): (List[Int], Int) = (xs.tail, xs.head) | |
val s = for { | |
a <- State(selectS) | |
b <- State(selectS) | |
} yield Map('a' -> a, 'b' -> b) | |
val ts1 = s.eval(List(1, 2)) ==== Map('a' -> 1, 'b' -> 2) | |
val ts2 = s.eval(List(1, 2, 3)) ==== Map('a' -> 1, 'b' -> 2) | |
// --------------------------------------------------------------------------- | |
import scalaz.StateT | |
import scalaz.std.list._ | |
def selectSL[A](xs: List[A]): List[(List[A], A)] = | |
xs map { x => (xs.filterNot(_ == x), x) } | |
val sl = for { | |
a <- StateT(selectSL[Int]) | |
b <- StateT(selectSL[Int]) | |
} yield Map('a' -> a, 'b' -> b) | |
val exp: List[Map[Char, Int]] = List(Map('a' -> 1, 'b' -> 2), Map('a' -> 2, 'b' -> 1)) | |
val tsl1 = sl.eval(List(1, 2)) ==== List(Map('a' -> 1, 'b' -> 2), Map('a' -> 2, 'b' -> 1)) | |
// --------------------------------------------------------------------------- | |
type StateL[A] = StateT[List, List[Int], A] | |
val g = for { | |
a <- StateT(selectSL[Int]) | |
b <- StateT(selectSL[Int]) | |
c <- StateT(selectSL[Int]) | |
_ <- (a + b == c).guard[StateL](()) | |
_ <- (a > b).prevent[StateL](()) | |
} yield Map('a' -> a, 'b' -> b, 'c' -> c) | |
val tg1 = g.eval(1 |-> 3) ==== List(Map('a' -> 1, 'b' -> 2, 'c' -> 3)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment