Option¶
Types, constructors, and combinators for working with optional values. See Option concepts for a guided introduction.
Types¶
pyfect.option.Some
dataclass
¶
pyfect.option.Nothing
dataclass
¶
Constructors¶
pyfect.option.some(value)
¶
Create an Option containing a value.
pyfect.option.nothing()
¶
Create an Option representing the absence of a value.
Returns the NOTHING singleton.
pyfect.option.from_optional(value)
¶
Convert a Python optional value to an Option.
None becomes Nothing, any other value becomes Some.
Source code in src/pyfect/option.py
pyfect.option.lift_predicate(predicate)
¶
Lift a predicate into a function that returns an Option.
Returns a function that produces Some(value) if the predicate holds, or Nothing if it does not.
Example
Source code in src/pyfect/option.py
Guards¶
pyfect.option.is_some(option)
¶
Return True if the Option contains a value.
pyfect.option.is_nothing(option)
¶
Return True if the Option is Nothing.
Transforming¶
pyfect.option.map(f)
¶
Transform the value inside an Option.
Applies f to the value if Some, passes Nothing through unchanged.
Example
Source code in src/pyfect/option.py
pyfect.option.flat_map(f)
¶
Chain a computation that may itself return Nothing.
Applies f to the value if Some, returning the resulting Option directly. If Nothing, returns Nothing without calling f.
Example
Source code in src/pyfect/option.py
pyfect.option.filter(predicate)
¶
Keep the value only if it satisfies the predicate.
If Some and predicate returns True, returns the Option unchanged. If Some and predicate returns False, returns Nothing. If Nothing, returns Nothing.
Example
Source code in src/pyfect/option.py
Extracting¶
pyfect.option.get_or_else(default)
¶
Return the value if Some, or the result of calling default if Nothing.
The default thunk is only evaluated when the Option is Nothing.
Example
Source code in src/pyfect/option.py
pyfect.option.get_or_none(opt)
¶
Return the value if Some, or None if Nothing.
Useful for interoperating with code that uses None to represent the absence of a value.
Example
Source code in src/pyfect/option.py
pyfect.option.get_or_raise(opt)
¶
Return the value if Some, or raise ValueError if Nothing.
Example
Source code in src/pyfect/option.py
Fallback¶
pyfect.option.or_else(alternative)
¶
Return the Option unchanged if Some, or the result of calling alternative if Nothing.
The alternative thunk is only evaluated when the Option is Nothing.
Example
Source code in src/pyfect/option.py
pyfect.option.first_some_of(options)
¶
Return the first Some in an iterable, or Nothing if there are none.
Short-circuits on the first Some found.
Example
Source code in src/pyfect/option.py
Combining¶
pyfect.option.zip_with(opt_a, opt_b, f)
¶
Combine two Options using a function.
If both are Some, applies f to their values and returns Some(result). If either is Nothing, returns Nothing.
Example
Source code in src/pyfect/option.py
pyfect.option.all(options)
¶
Combine a list or dict of Options into a single Option.
If all elements are Some, returns Some containing the collected values. If any element is Nothing, returns Nothing.
Note
For heterogeneous collections (elements with different value types), the type checker will not infer the correct type automatically. You should provide an explicit type annotation and suppress the error: