Funx.Monad.Effect.Right
Mix.install([
{:funx,
git: "https://github.com/JKWA/funx.git",
branch: "main"
}
])
Overview
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/2functions to handle monadic operations within an effectful, lazy execution context. -
String.Chars: Provides ato_string/1function to representRightvalues 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
-
ask/1– Returns the environment passed torun/2as aRight. -
asks/2– Applies a function to the environment passed torun/2, wrapping the result in aRight.
Function Examples
import Funx.Monad.Effect.Right
alias Funx.Monad.Effect.Right
Functions
pure/2
Creates a new Right effect.
The pure/2 function wraps a value in the Right effect monad, representing an asynchronous success.
Examples
effect = pure("success")
Funx.Monad.Effect.run(effect)
ask/1
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
ask()
|> Funx.Monad.map(& &1[:user])
|> Funx.Monad.Effect.run(%{user: "alice"})
asks/2
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
asks(fn env -> env[:user] end)
|> Funx.Monad.bind(fn user -> Funx.Monad.Effect.right(user) end)
|> Funx.Monad.Effect.run(%{user: "alice"})