y-yu / atnos-eff-state

State without mutable using atnos-eff.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

State without mutable using atnos-eff

CI

PoC of State without mutable(e.g. var variables) using atnos-eff. Its monadic and applicative laws are tested by ScalaCheck and cats-laws.

Example

See also: Main.scala.

type R = Fx.fx2[State[Int, *], Either[Throwable, *]]

val eff: Eff[R, Int] = for {
  s1 <- get[Int, R]
  _ = println(s"s1: $s1")
  num <- EitherEffect.fromEither[R, Throwable, Int](
    Right(5)
  )
  _ <- set[Int, R](1)
  s2 <- get[Int, R]
  _ = println(s"s2: $s2")
} yield s1 + num

val result = Eff.run(
  EitherEffect.runEither(
    eff.runState(5)
  )
)
println(s"result(s1 + num): $result")

Output:

s1: 5
s2: 1
result(s1 + num): Right((10,1))

Tests

See StateLawsTest.scala.

About

State without mutable using atnos-eff.

License:MIT License


Languages

Language:Scala 100.0%