View Source Funx.Monad.Effect.Right (funx v0.1.0)
Represents the Right
variant of the Effect
monad, used to model a successful computation in an asynchronous context.
This module implements the following protocols:
Funx.Monad
: Implements thebind/2
,map/2
, andap/2
functions to handle monadic operations within an effectful, lazy execution context.String.Chars
: Provides ato_string/1
function to representRight
values as strings.
The Right
effect allows the computation to proceed with successful values, supporting lazy, asynchronous tasks
and capturing execution context through the Effect.Context
struct.
Reader Operations
Summary
Functions
Returns a Funx.Monad.Effect.Right
that yields the environment passed to Funx.Monad.Effect.run/2
.
Returns a Funx.Monad.Effect.Right
that applies the given function to the environment passed to Funx.Monad.Effect.run/2
.
Creates a new Right
effect.
Types
@type t(right) :: %Funx.Monad.Effect.Right{ context: Funx.Monad.Effect.Context.t(), effect: (term() -> Task.t()) | (term() -> Funx.Monad.Either.Right.t(right)) }
Represents an asynchronous computation that produces a Right
value.
The effect
function is typically a deferred task that takes an environment and returns a Task
.
Since Elixir does not support parameterized Task.t()
types, the return type is described as a union:
either a Task.t()
or a plain Either.Right.t(right)
for testability and flexibility.
The context
carries telemetry and trace information used during execution.
Functions
@spec ask(Funx.Monad.Effect.Context.opts_or_context()) :: t(env) when env: term()
Returns a Funx.Monad.Effect.Right
that yields the environment passed to Funx.Monad.Effect.run/2
.
This is the Reader monad's equivalent of ask
, giving access to the entire injected environment
for further computation.
Example
iex> Funx.Monad.Effect.Right.ask()
...> |> Funx.Monad.map(& &1[:user])
...> |> Funx.Monad.Effect.run(%{user: "alice"})
%Funx.Monad.Either.Right{right: "alice"}
@spec asks( (Funx.Monad.Effect.Context.t() -> result), Funx.Monad.Effect.Context.opts_or_context() ) :: t(result) when result: term()
Returns a Funx.Monad.Effect.Right
that applies the given function to the environment passed to Funx.Monad.Effect.run/2
.
This allows extracting a value from the environment and using it in an effectful computation, following the Reader pattern.
Example
iex> Funx.Monad.Effect.Right.asks(fn env -> env[:user] end)
...> |> Funx.Monad.bind(fn user -> Funx.Monad.Effect.right(user) end)
...> |> Funx.Monad.Effect.run(%{user: "alice"})
%Funx.Monad.Either.Right{right: "alice"}
@spec pure(right, Funx.Monad.Effect.Context.opts_or_context()) :: t(right) when right: term()
Creates a new Right
effect.
The pure/2
function wraps a value in the Right
effect monad, representing an asynchronous success.
Examples
iex> effect = Funx.Monad.Effect.Right.pure("success")
iex> Funx.Monad.Effect.run(effect)
%Funx.Monad.Either.Right{right: "success"}