Skip to content

Result Implementation

monad_std.result.Ok

Bases: Generic[KT, KE], Result[KT, KE]

Source code in monad_std/result.py
class Ok(t.Generic[KT, KE], Result[KT, KE]):
    __value: KT

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

    def __repr__(self):
        return f"Result::Ok({self.__value})"

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

    def __eq__(self, other):
        return isinstance(other, Ok) and self.__value == other.__value

    def __instancecheck__(self, instance):
        return isinstance(instance, Ok)

    def is_ok(self) -> bool:
        return True

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

    def is_err(self) -> bool:
        return False

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

    def ok(self) -> "Option[KT]":
        return Option.some(self.__value)

    def err(self) -> "Option[KE]":
        return Option.none()

    def map(self, func: t.Callable[[KT], U]) -> Result[U, KE]:
        return Result.of_ok(func(self.__value))

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

    def map_err_mut(self, func: t.Callable[[KE], None]) -> Result[KT, KE]:
        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[[KE], U], func: t.Callable[[KT], U]) -> U:
        return func(self.__value)

    def map_err(self, func: t.Callable[[KE], F]) -> Result[KT, F]:
        return Result.of_ok(self.__value)

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

    def inspect_err(self, func: t.Callable[[KE], None]) -> Result[KT, KE]:
        return self

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

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

    def expect_err(self, msg: str) -> KE:
        raise UnwrapException("Result", msg + f': {repr(self.__value)}')

    def to_pattern(self) -> t.Tuple[bool, t.Union[KT, KE]]:
        return True, self.__value

    def to_either(self) -> "Left[KT, KE]":
        return Left(self.__value)

    def unwrap_unchecked(self) -> t.Union[KT, KE]:
        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, op: t.Callable[[KE], KT]) -> KT:
        return self.__value

    def unwrap_err(self) -> KE:
        raise UnwrapException("Result", f"call `Result.unwrap_err` on an `Ok` value: {self.__value}")

    def bool_and(self, res: Result[U, KE]) -> Result[U, KE]:
        if res.is_ok():
            return Result.of_ok(res.unwrap())
        else:
            return Result.of_err(res.unwrap_err())

    def and_then(self, op: t.Callable[[KT], Result[U, KE]]) -> Result[U, KE]:
        return op(self.__value)

    def bool_or(self, res: Result[KT, F]) -> Result[KT, F]:
        return Result.of_ok(self.__value)

    def or_else(self, op: t.Callable[[KE], Result[KT, F]]) -> Result[KT, F]:
        return Result.of_ok(self.__value)

monad_std.result.Err

Bases: Generic[KT, KE], Result[KT, KE]

Source code in monad_std/result.py
class Err(t.Generic[KT, KE], Result[KT, KE]):
    __value: KE

    def __init__(self, value: KE):
        self.__value = value

    def __repr__(self):
        return f"Result::Err({self.__value})"

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

    def __eq__(self, other):
        return isinstance(other, Err) and self.__value == other.__value

    def __instancecheck__(self, instance):
        return isinstance(instance, Err)

    def is_ok(self) -> bool:
        return False

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

    def is_err(self) -> bool:
        return True

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

    def ok(self) -> "Option[KT]":
        return Option.none()

    def err(self) -> "Option[KE]":
        return Option.some(self.__value)

    def map(self, func: t.Callable[[KT], U]) -> Result[U, KE]:
        return Result.of_err(self.__value)

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

    def map_err_mut(self, func: t.Callable[[KE], None]) -> Result[KT, KE]:
        func(self.__value)
        return self

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

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

    def map_err(self, func: t.Callable[[KE], F]) -> Result[KT, F]:
        return Result.of_err(func(self.__value))

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

    def inspect_err(self, func: t.Callable[[KE], None]) -> Result[KT, KE]:
        func(self.__value)
        return self

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

    def expect(self, msg: str) -> KT:
        raise UnwrapException("Result", msg + f': {repr(self.__value)}')

    def expect_err(self, msg: str) -> KE:
        return self.__value

    def to_pattern(self) -> t.Tuple[bool, t.Union[KT, KE]]:
        return False, self.__value

    def to_either(self) -> "Right[KT, KE]":
        return Right(self.__value)

    def unwrap_unchecked(self) -> t.Union[KT, KE]:
        return self.__value

    def unwrap(self) -> KT:
        raise UnwrapException("Result", f"call `Result.unwrap` on an `Err` value: {repr(self.__value)}")

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

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

    def unwrap_err(self) -> KE:
        return self.__value

    def bool_and(self, res: Result[U, KE]) -> Result[U, KE]:
        return Result.of_err(self.__value)

    def and_then(self, op: t.Callable[[KT], Result[U, KE]]) -> Result[U, KE]:
        return Result.of_err(self.__value)

    def bool_or(self, res: Result[KT, F]) -> Result[KT, F]:
        if res.is_ok():
            return Result.of_ok(res.unwrap())
        else:
            return Result.of_err(res.unwrap_err())

    def or_else(self, op: t.Callable[[KE], Result[KT, F]]) -> Result[KT, F]:
        return op(self.__value)