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()