Last active
October 7, 2015 11:18
-
-
Save retronym/3157243 to your computer and use it in GitHub Desktop.
filterM / State
This file contains 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
scala> type RunState[A] = State[(Int, Int), A] // ((lastNum, run length), A)defined type alias RunState | |
scala> val ls = List(1, 1, 2, 2, 2, 3, 3, 3, 3, 1, 1) | |
ls: List[Int] = List(1, 1, 2, 2, 2, 3, 3, 3, 3, 1, 1) | |
scala> std.list.filterM[Int, RunState](ls)(x => | |
State { case (y, n) => | |
val nn = if (x == y) n + 1 else 0 | |
((x, nn), nn == 0) } | |
).eval((ls.head, -1)) | |
res29: scalaz.Id.Id[List[Int]] = List(1, 2, 3, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment