Option
- class towhee.functional.Option[source]
Bases:
Generic
[towhee.functional.option.A
]Functional-style error handling.
Option[A] = Some(A) or Empty[A] 1. Some(A): just a container for result; 2. Empty[A]: result is empty, because of input error or computation error;
Examples:
>>> a = Some(10) >>> a.map(lambda x: x/2.0) Some(5.0)
>>> a.map(lambda x: x/0) Empty()
>>> b = Empty() >>> b.map(lambda x: x/2.0) Empty()
- static of(x: towhee.functional.option.T)[source]
Return a boxed value
- Parameters
x (T) – input value
- Returns
boxed value
- Return type
Some(T)
- flat_map(f: Callable[[towhee.functional.option.A], towhee.functional.option.Option[towhee.functional.option.B]]) towhee.functional.option.Option[towhee.functional.option.B] [source]
Apply boxed version of callable
- Parameters
f (Callable[[A], Option[B]]) – boxed version of callable
- Returns
boxed value
- Return type
Option[B]
Examples:
>>> Option.of(1).flat_map(lambda x: x+1) 2
>>> Option.empty().flat_map(lambda x: x+1) Empty()
- map(f: Callable[[towhee.functional.option.A], towhee.functional.option.B]) towhee.functional.option.Option[towhee.functional.option.B] [source]
Apply function to value
- Parameters
f (Callable[[A], B]) – unboxed function
- Returns
boxed return value
- Return type
Option[B]
- get_or_else(default)[source]
Return unboxed value, or default is the value is empty.
Examples:
>>> Option.of(0).get_or_else(1) 0
>>> Option.empty().get_or_else(1) 1
- __weakref__
list of weak references to the object (if defined)
- class towhee.functional.Some(x: towhee.functional.option.A)[source]
Bases:
towhee.functional.option.Option
[towhee.functional.option.A
]Some value for Option
- flat_map(f: Callable[[towhee.functional.option.A], towhee.functional.option.Option[towhee.functional.option.B]]) towhee.functional.option.Option[towhee.functional.option.B] [source]
Apply boxed version of callable
- Parameters
f (Callable[[A], Option[B]]) – boxed version of callable
- Returns
boxed value
- Return type
Option[B]
Examples:
>>> Option.of(1).flat_map(lambda x: x+1) 2
>>> Option.empty().flat_map(lambda x: x+1) Empty()
- class towhee.functional.Empty(x: Optional[Any] = None, e: Optional[Exception] = None)[source]
Bases:
towhee.functional.option.Option
[towhee.functional.option.A
]Empty value for Option
- flat_map(f: Callable[[towhee.functional.option.A], towhee.functional.option.Option[towhee.functional.option.B]]) towhee.functional.option.Option[towhee.functional.option.B] [source]
Apply boxed version of callable
- Parameters
f (Callable[[A], Option[B]]) – boxed version of callable
- Returns
boxed value
- Return type
Option[B]
Examples:
>>> Option.of(1).flat_map(lambda x: x+1) 2
>>> Option.empty().flat_map(lambda x: x+1) Empty()