Execute an operation requiring some additional context A
provided within this Ref.
Execute an operation requiring some additional context A
provided within this Ref.
The value inside Ref cannot be modified by this operation (see runTell or runState for such cases) but it can be modified concurrently by a forked task, in which case reads will see the updated value.
case class RequestId(text: String) def greet[F[_]: Sync: ApplicativeAsk[?[_], RequestId]](name: String): F[String] = for { rId <- ApplicativeAsk.askF[F]() _ <- Sync[F].delay(println(s"Handling request $rId")) } yield s"Hello, $name" for { id <- IO(UUID.randomUUID().toString).map(RequestId) ref <- Ref[IO].of(id) res <- ref.runAsk { implicit aa => greet("Oleg") } } yield res
Execute a stateful operation using this Ref
to store / update state.
Execute a stateful operation using this Ref
to store / update state.
The Ref will be modified to contain the resulting value. Returning value
would be a result of function passed to runState.
def getAndIncrement[F[_]: Apply](implicit MS: MonadState[F, Int]) = MS.get <* MS.modify(_ + 1) for { ref <- Ref.of[IO](0) out <- ref.runState { implicit ms => getAndIncrement[IO].replicateA(3).as("Done") } state <- ref.get } yield (out, state) == ("Done", 3)
Execute an operation requiring ability to "log" values of type A
, and,
potentially, read current value.
Execute an operation requiring ability to "log" values of type A
, and,
potentially, read current value.
The operation requires A
to have a Semigroup
instance. Unlike standard
Writer
monad, initial (zero) is not required.
def generateUser[F[_]: Sync: FunctorTell[?[_], String]](login: String) = for { _ <- tellF[F](s"Starting key generation for $login") pwd <- IO(Random.alphanumeric.take(16).mkString) _ <- tellF[F](s"Generated key: $key") } yield (login, pwd) for { ref <- Ref[IO].of(NonEmptyList.of("Operation started")) user <- ref.runTell { implicit ft => generateUser("Alice") } log <- ref.get _ <- IO(println(log)) } yield user
Consumer if you're interested in simply performing an operation
on each tell