Effect¶
Constructors for building effects and interop with other types.
All functions in this module are re-exported from pyfect.effect.
Constructors¶
pyfect.effect.succeed(value)
¶
Create an effect that succeeds with a value.
pyfect.effect.fail(error)
¶
Create an effect that fails with an error.
pyfect.effect.sync(thunk)
¶
Create an effect from a synchronous computation.
The computation is not executed immediately - it's deferred until the effect is run by the runtime.
Example
Source code in src/pyfect/effect.py
pyfect.effect.async_(thunk)
¶
Create an effect from an asynchronous computation.
The computation is not executed immediately - it's deferred until the effect is run by the runtime.
Note: Uses async_ (with underscore) since 'async' is a Python keyword.
Example
Source code in src/pyfect/effect.py
pyfect.effect.try_sync(thunk)
¶
Create an effect from a synchronous computation that might throw.
Exceptions are captured and converted to effect errors.
Example
Source code in src/pyfect/effect.py
pyfect.effect.try_async(thunk)
¶
Create an effect from an asynchronous computation that might throw.
Exceptions are captured and converted to effect errors.
Example
Source code in src/pyfect/effect.py
pyfect.effect.suspend(thunk)
¶
Delay the creation of an effect until runtime.
The thunk is called each time the effect is run, allowing for: - Lazy evaluation of effects - Re-execution of side effects on each run - Capturing fresh state each time
Example
Source code in src/pyfect/effect.py
Interop¶
pyfect.effect.from_option(error)
¶
Convert an Option into an Effect.
Some(value) becomes a successful effect with that value. Nothing becomes a failed effect using the provided error thunk.
The error thunk is only called when the Option is Nothing.
Example
Source code in src/pyfect/effect.py
pyfect.effect.from_either(e)
¶
Convert an Either into an Effect.
Right(value) becomes a successful effect with that value. Left(value) becomes a failed effect with that value as the error.