# Untitled

By: a guest on Aug 21st, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 22  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. scala> import scalaz._
2. import scalaz._
3.
4. scala> import Scalaz._
5. import Scalaz._
6.
7. scala> case class FooState(partialResult: List[Int], result: List[List[Int]]) {
8.      |   def newPartial(n:Int) = FooState(n::partialResult, result)
9.      |   def newResult = FooState(List(), partialResult.reverse :: result)
10.      | }
11. defined class FooState
12.
13. scala> def nextFooState(n: Option[Int]) = modify( (s:FooState) => n match {
14.      |   case None => s.newResult
15.      |   case Some(nn) => s.newPartial(nn)
16.      | })
17. nextFooState: (n: Option[Int])scalaz.package.State[FooState,FooState]
18.
19. scala> def chunks(l: List[Option[Int]]) : List[List[Int]] = {
20.      |   val initialState = FooState(List(),List())
21.      |   val finalState = l.traverseS(nextFooState).exec(initialState).newResult
22.      |   finalState.result.reverse
23.      | }
24. chunks: (l: List[Option[Int]])List[List[Int]]
25.
26. scala> chunks(List(Some(1), Some(2), None, Some(3), Some(4), Some(5), None, Some(6),Some(7)))
27. res0: List[List[Int]] = List(List(1, 2), List(3, 4, 5), List(6, 7))