Skip to content

Instantly share code, notes, and snippets.

@retronym
Last active October 7, 2015 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retronym/3157243 to your computer and use it in GitHub Desktop.
Save retronym/3157243 to your computer and use it in GitHub Desktop.
filterM / State
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