monad_std.option.Option
Bases: Generic[KT]
Option
monad for python.
__add__(other)
Alias self.__value.__add__
.
Returns:
Type | Description |
---|---|
Option[Any]
|
If both value are |
__and__(other)
Alias bool_and
.
__bool__()
abstractmethod
Returns False
only if contained value is None
.
__hash__()
abstractmethod
hash(Option)
has the same result as its contained value.
__mul__(other)
Alias self.__value.__mul__
.
Returns:
Type | Description |
---|---|
Option[Any]
|
If both value are |
__or__(other)
Alias bool_or
.
__xor__(other)
Alias bool_xor
.
and_then(func)
abstractmethod
Returns None
if the option is None
,
otherwise calls func
with the wrapped value and returns the result. Alias flatmap
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], Option[U]]
|
A callable object to produce the next value. |
required |
Examples:
None
.
bool_and(optb)
abstractmethod
Returns None if the option is None
, otherwise returns optb
. Alias &(__and__)
.
and
is a keyword in Python, so here we use bool_and
instead.
Arguments passed to and are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use and_then
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optb |
Option[U]
|
Another option object to do the bool evaluation. |
required |
Examples:
bool_or(optb)
abstractmethod
Returns the option if it contains a value, otherwise returns optb
. Alias ||(__or__)
.
or
is a keyword in Python, so here we use bool_or
instead.
Arguments passed to or are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use or_else
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optb |
Option[KT]
|
Another option object to do the bool evaluation. |
required |
Examples:
bool_xor(optb)
abstractmethod
Returns Some
if exactly one of self
, optb
is Some
, otherwise returns None
. Alias ^(__xor__)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optb |
Option[KT]
|
Another option object to do the bool evaluation. |
required |
Examples:
clone()
Clone self.
clone_from(value)
staticmethod
Clone an Option
.
expect(msg)
abstractmethod
Returns the contained Some
value.
Returns the contained Some
value, and raise an exception if the value is a None
with a custom panic
message provided by msg.
Recommended Message Style
We recommend that expect
messages are used to describe the reason you expect the Option
should be Some
.
Hint: If you’re having trouble remembering how to phrase expect error messages, remember to focus on the word “should” as in “env variable should be set by blah” or “the given binary should be available and executable by the current user”.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
The message to display if this option is none. |
required |
Returns:
Type | Description |
---|---|
KT
|
The contained |
Raises:
Type | Description |
---|---|
UnwrapException
|
Raise an exception if the value is a |
Examples:
filter(func)
abstractmethod
Filter the option.
Returns None
if the option is None
, otherwise calls predicate with the wrapped value and returns:
Some(t)
if predicate returnsTrue
(wheret
is the wrapped value), andNone
if predicate returnsFalse
.
This function works similar to builtin.filter()
.
You can imagine the Option<KT>
being an iterator over one or zero elements.
filter()
lets you decide which elements to keep.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], bool]
|
The callable object decides whether the element should be kept. |
required |
Examples:
flatmap(func)
abstractmethod
Alias of and_then
.
flatten()
Converts from Option<Option<KT>>
to Option<KT>
.
Examples:
from_nullable(value)
staticmethod
Construct an Option
from a nullable value.
inspect(func)
abstractmethod
Calls the provided closure with the contained value (if Some
), and return the option itself.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], None]
|
A callable object which accepts the wrapped value and returns nothing. |
required |
Examples:
is_none()
abstractmethod
is_some()
abstractmethod
is_some_and(func)
abstractmethod
Returns true if the option is a Some
and the value inside it matches a predicate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], bool]
|
A callable object which accepts the not-none value of the option and returns a boolean. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the option is a |
Examples:
map(func)
abstractmethod
Maps an Option<KT>
to Option<U>
by applying a function
to a contained value (if Some
) or returns None
(if None
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], U]
|
A callable object that accepts the wrapped value and returns the processed value. |
required |
Returns:
Type | Description |
---|---|
Option[U]
|
Returns |
Examples:
map_mut(func)
abstractmethod
Maps and Option<KT>
by changing its wrapped value.
This method require a function to return nothing, and passes a reference into it. Actually, python doesn't differentiate mutable / immutable value, and this method is just for explicit notification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[KT], None]
|
A callable object that accepts the wrapped value. |
required |
Examples:
map_or(default, func)
abstractmethod
Returns the provided default result (if none), or applies a function to the contained value (if any).
Arguments passed to map_or are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use map_or_else
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
U
|
The default value if the option is |
required |
func |
Callable[[KT], U]
|
The function to apply to the contained value. |
required |
Examples:
map_or_else(default, func)
abstractmethod
Computes a default function result (if none), or applies a different function to the contained value (if any).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
Callable[[], U]
|
The function to produce a default value. |
required |
func |
Callable[[KT], U]
|
The function to apply to the contained value. |
required |
Examples:
none()
staticmethod
ok_or(err)
abstractmethod
Transforms the Option<KT>
into a Result<KT, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err)
.
Arguments passed to ok_or are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use ok_or_else
, which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
err |
E
|
A value representing an error |
required |
Examples:
ok_or_else(err)
abstractmethod
Transforms the Option<KT>
into a Result<KT, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err())
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
err |
Callable[[], E]
|
A callable object that returns an error value. |
required |
Examples:
or_else(func)
abstractmethod
Returns the option if it contains a value, otherwise calls func
and returns the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[], Option[KT]]
|
A callable object to produce the next value. |
required |
Examples:
some(value)
staticmethod
Create a value of Some(KT)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
KT
|
Some value of type |
required |
Returns:
Type | Description |
---|---|
Option[KT]
|
|
to_array()
abstractmethod
to_iter()
Alias iter(self.to_array())
.
to_nullable()
abstractmethod
Returns the contained value. If self
is an Option::None
, this will return Python's None
directly.
Note: If the wrapped object is None
itself, this will also return None
as it's wrapped by the Some
.
It's impossible to distinguish between Option::Some(None)
and Option::None
by using this method.
Returns:
Type | Description |
---|---|
Optional[KT]
|
The contained value. |
Examples:
to_pattern()
abstractmethod
Returns the contained value for pattern-matching.
This is the same as Option.to_nullable()
.
transpose()
unwrap()
abstractmethod
Returns the contained Some
value.
Returns:
Type | Description |
---|---|
KT
|
The wrapped |
Raises:
Type | Description |
---|---|
UnwrapException
|
Raise an exception if the value is a |
Examples:
unwrap_or(default)
abstractmethod
Returns the contained Some
value or a provided default.
Arguments passed to unwrap_or
are eagerly evaluated.
If you are passing the result of a function call,
it is recommended to use unwrap_or_else
,
which is lazily evaluated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
KT
|
The default value. |
required |
Returns:
Type | Description |
---|---|
KT
|
The contained |
Examples:
unwrap_or_else(func)
abstractmethod
Returns the contained Some
value or computes it from a callable object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[[], KT]
|
A callable object to compute. |
required |
Examples:
unwrap_unchecked()
abstractmethod
Returns the contained value. If self
is an Option::None
, this will return Python's None
directly.
This is the same as Option.to_nullable()
.
However, this function's signature is -> KT
instead of Optional[KT]
.
unzip()
Unzips an option containing a tuple of two options.
If self is Some((a, b))
this method returns (Some(a), Some(b))
. Otherwise, (None, None)
is returned.
Examples:
zip(other)
abstractmethod
zip_with(other, func)
abstractmethod
Zips self
and another Option
with a callable object.
If self is Some(s)
and other is Some(o)
, this method returns Some(f(s, o))
. Otherwise, None
is returned.
Examples: