Guest User

Untitled

a guest
Dec 16th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. def main(args: Array[String]): Unit = {
  2.  
  3.  
  4. val stateProgram : IndexedStateT[Eval, Counter, Counter, Int] = for {
  5. _ <- increaseCount(10)
  6. count1 <- getCount
  7. _ <- increaseCount(5)
  8. count2 <- getCount
  9. } yield count1 + count2
  10.  
  11. stateProgram.run(Counter(100)).map(state => IO(println(state))).value.unsafeRunSync()
  12. }
  13.  
  14. case class Counter(int: Int = 0)
  15.  
  16. def increaseCount(by: Int = 1): State[Counter, Unit] = State[Counter, Unit](counter =>
  17. (counter.copy(counter.int + by), ())
  18. )
  19.  
  20. def getCount: State[Counter, Int] = State[Counter, Int](c => (c, c.int))
Add Comment
Please, Sign In to add comment