Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.effect.IO
- import cats.effect.concurrent.Ref
- import cats.data.State
- import scala.util.Random
- object Awesome extends App {
- case class Output(last: Option[String], count: Int)
- case class MyState(strings: List[String])
- object MyState {
- def init: MyState = MyState(List.empty[String])
- }
- val ref = Ref.of[IO, MyState](MyState.init).unsafeRunSync()
- def addWord(word: String): State[MyState, Output] = State[MyState, Output] { s =>
- s.copy(strings = s.strings :+ word) -> Output(s.strings.lastOption, s.strings.size)
- }
- def program: IO[Unit] =
- for {
- str <- IO(Random.nextString(10))
- out <- ref.modifyState(addWord(str))
- st <- ref.get
- _ <- IO(println(s"Current Output $out"))
- _ <- IO(println(s"State $st"))
- _ <- program
- } yield ()
- program.unsafeRunSync()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement