public
Last active

filterM / State

  • Download Gist
filterM.scala
Scala
1 2 3 4 5 6 7 8 9 10 11
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.