Skip to content

Option Implementation

monad_std.option.OpSome

Bases: Generic[KT], Option[KT]

Source code in monad_std/option.py
class OpSome(t.Generic[KT], Option[KT]):
    __value: KT

    def __init__(self, __value: KT):
        self.__value = __value

    def __bool__(self):
        return True

    def __str__(self):
        return str(self.__value)

    def __repr__(self):
        return f'Option::Some({self.__value})'

    def __eq__(self, other):
        if isinstance(other, OpSome):
            return self.__value == other.__value
        else:
            return False

    def __hash__(self):
        return hash(self.__value)

    def clone(self):
        return OpSome(self.__value)

    def is_some(self) -> bool:
        return True

    def is_none(self) -> bool:
        return False

    def is_some_and(self, func: t.Callable[[KT], bool]) -> bool:
        return func(self.__value)

    def expect(self, msg: str) -> KT:
        return self.__value

    def to_nullable(self) -> t.Optional[KT]:
        return self.__value

    def unwrap(self) -> KT:
        return self.__value

    def unwrap_or(self, default: KT) -> KT:
        return self.__value

    def unwrap_or_else(self, func: t.Callable[[], KT]) -> KT:
        return self.__value

    def unwrap_unchecked(self) -> KT:
        return self.__value

    def to_pattern(self) -> t.Optional[KT]:
        return self.__value

    def inspect(self, func: t.Callable[[KT], None]) -> Option[KT]:
        func(self.__value)
        return self

    def map(self, func: t.Callable[[KT], U]) -> Option[U]:
        return Option.some(func(self.__value))

    def map_mut(self, func: t.Callable[[KT], None]) -> Option[KT]:
        func(self.__value)
        return self

    def map_or(self, default: U, func: t.Callable[[KT], U]) -> U:
        return func(self.__value)

    def map_or_else(self, default: t.Callable[[], U], func: t.Callable[[KT], U]) -> U:
        return func(self.__value)

    def ok_or(self, err: E) -> "Result[KT, E]":
        return Result.of_ok(self.__value)

    def ok_or_else(self, err: t.Callable[[], E]) -> "Result[KT, E]":
        return Result.of_ok(self.__value)

    def to_array(self) -> t.List[KT]:
        return [self.__value]

    def bool_and(self, optb: Option[U]) -> Option[U]:
        return optb.clone()

    def and_then(self, func: t.Callable[[KT], Option[U]]) -> Option[U]:
        return func(self.__value)

    def flatmap(self, func: t.Callable[[KT], Option[U]]) -> Option[U]:
        return func(self.__value)

    def bool_or(self, optb: Option[KT]) -> Option[KT]:
        return self.clone()

    def or_else(self, func: t.Callable[[], Option[KT]]) -> Option[KT]:
        return self.clone()

    def bool_xor(self, optb: Option[KT]) -> Option[KT]:
        if optb.is_some():
            return OpNone()
        else:
            return self.clone()

    def filter(self, func: t.Callable[[KT], bool]) -> Option[KT]:
        if func(self.__value):
            return self.clone()
        else:
            return OpNone()

    def zip(self, other: Option[U]) -> Option[t.Tuple[KT, U]]:
        if other.is_some():
            return OpSome((self.__value, other.unwrap()))
        else:
            return OpNone()

    def zip_with(self, other: Option[U], func: t.Callable[[KT, U], Option[R]]) -> Option[R]:
        if other.is_some():
            return func(self.__value, other.unwrap_unchecked())
        else:
            return OpNone()

monad_std.option.OpNone

Bases: Generic[KT], Option[KT]

Source code in monad_std/option.py
class OpNone(t.Generic[KT], Option[KT]):
    def __bool__(self):
        return False

    def __str__(self):
        return 'None'

    def __repr__(self):
        return 'Option::None'

    def __eq__(self, other):
        return isinstance(other, OpNone)

    def __hash__(self):
        return hash(None)

    def clone(self):
        return self

    def is_some(self) -> bool:
        return False

    def is_none(self) -> bool:
        return True

    def is_some_and(self, func: t.Callable[[KT], bool]) -> bool:
        return False

    def expect(self, msg: str) -> KT:
        raise UnwrapException("Option", msg)

    def to_nullable(self) -> t.Optional[KT]:
        return None

    def unwrap(self) -> KT:
        raise UnwrapException("Option", "call `Option.unwrap` on an `Option::None` object")

    def unwrap_or(self, default: KT) -> KT:
        return default

    def unwrap_or_else(self, func: t.Callable[[], KT]) -> KT:
        return func()

    def unwrap_unchecked(self) -> KT:
        # This is safe because the api asks the caller
        # to guarentee the call's safety.
        return None # type: ignore[return-value]

    def to_pattern(self) -> t.Optional[KT]:
        return None

    def inspect(self, func: t.Callable[[KT], None]) -> Option[KT]:
        return self

    def map(self, func: t.Callable[[KT], U]) -> Option[U]:
        return Option.none()

    def map_mut(self, func: t.Callable[[KT], None]) -> Option[KT]:
        return Option.none()

    def map_or(self, default: U, func: t.Callable[[KT], U]) -> U:
        return default

    def map_or_else(self, default: t.Callable[[], U], func: t.Callable[[KT], U]) -> U:
        return default()

    def ok_or(self, err: E) -> "Result[KT, E]":
        return Result.of_err(err)

    def ok_or_else(self, err: t.Callable[[], E]) -> "Result[KT, E]":
        return Result.of_err(err())

    def to_array(self) -> t.List[KT]:
        return []

    def bool_and(self, optb: Option[U]) -> Option[U]:
        return Option.none()

    def and_then(self, func: t.Callable[[KT], Option[U]]) -> Option[U]:
        return Option.none()

    def flatmap(self, func: t.Callable[[KT], Option[U]]) -> Option[U]:
        return Option.none()

    def bool_or(self, optb: Option[KT]) -> Option[KT]:
        return optb.clone()

    def or_else(self, func: t.Callable[[], Option[KT]]) -> Option[KT]:
        return func()

    def bool_xor(self, optb: Option[KT]) -> Option[KT]:
        return optb.clone()

    def filter(self, func: t.Callable[[KT], bool]) -> Option[KT]:
        return self

    def zip(self, other: Option[U]) -> Option[t.Tuple[KT, U]]:
        return Option.none()

    def zip_with(self, other: Option[U], func: t.Callable[[KT, U], Option[R]]) -> Option[R]:
        return Option.none()