Class

com.olegpy.meow.effects

RefEffects

Related Doc: package effects

Permalink

implicit final class RefEffects[F[_], A] extends AnyVal

Linear Supertypes
AnyVal, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. RefEffects
  2. AnyVal
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new RefEffects(self: Ref[F, A])

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def getClass(): Class[_ <: AnyVal]

    Permalink
    Definition Classes
    AnyVal → Any
  6. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  7. def runAsk[B](f: (ApplicativeAsk[F, A]) ⇒ B)(implicit F: Applicative[F]): B

    Permalink

    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
  8. def runState[B](f: (MonadState[F, A]) ⇒ B)(implicit F: Monad[F]): B

    Permalink

    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)
  9. def runTell[B](f: (FunctorTell[F, A]) ⇒ B)(implicit F: Functor[F], A: Semigroup[A]): B

    Permalink

    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
    See also

    Consumer if you're interested in simply performing an operation on each tell

  10. val self: Ref[F, A]

    Permalink
  11. def toString(): String

    Permalink
    Definition Classes
    Any

Inherited from AnyVal

Inherited from Any

Ungrouped