Just#
- class Just[source]#
The just part of the Maybe monad.
-
value:
Final
#
- map(mapper)[source]#
Transform this just by applying
mapper
to its argument and wrapping the result in a newJust
.>>> from cg_maybe import Just, Nothing >>> Just(5).map(lambda el: el * el) Just(25) >>> Nothing.map(lambda el: el * el) Nothing
- map_or_default(mapper, default)[source]#
Transform this just by applying
mapper
to its argument and return the result.>>> from cg_maybe import Just, Nothing >>> Just(5).map_or_default(lambda el: el * el, 10) 25 >>> Nothing.map_or_default(lambda el: el * el, 10) 10
- chain(chainer)[source]#
Transforms
this
with a function that returns aMaybe
.>>> from cg_maybe import Just, Nothing >>> Just(5).chain(lambda el: Just(el * el)) Just(25) >>> Just(5).chain(lambda _: Nothing) Nothing >>> Nothing.chain(lambda el: Just(el * el)) Nothing >>> Nothing.chain(lambda _: Nothing) Nothing
- chain_nullable(chainer)[source]#
Similar to
chain
but for a function that returns an options.>>> from cg_maybe import Just, Nothing >>> Just(5).chain_nullable(lambda el: el * el) Just(25) >>> Just(5).chain_nullable(lambda _: None) Nothing >>> Just(5).chain_nullable(lambda el: Just(el * el)) Just(Just(25)) >>> Nothing.chain_nullable(lambda el: el * el) Nothing >>> Nothing.chain_nullable(lambda _: None) Nothing
- alt(alternative)[source]#
Return the given
alternative
if called on aNothing
, otherwise the method returns the value it is called on.>>> from cg_maybe import Just, Nothing >>> Just(5).alt(Just(10)) Just(5) >>> Nothing.alt(Just(10)) Just(10)
- alt_lazy(maker)[source]#
Return the result of
maker
if called on aNothing
, otherwise the method returns the value it is called on.>>> from cg_maybe import Just, Nothing >>> Just(5).alt_lazy(lambda: print(10)) Just(5) >>> Nothing.alt_lazy(lambda: [print(10), Just(15)][1]) 10 Just(15)
- unsafe_extract()[source]#
Get the value from a
Just
, or raise if called on aNothing
.>>> from cg_maybe import Nothing, Just >>> Nothing.unsafe_extract() Traceback (most recent call last): ... AssertionError >>> Just(10).unsafe_extract() 10
- Return type:
_T
- or_default_lazy(producer)[source]#
Get the value from a
Just
, or return the given a default as produced by the given function.>>> from cg_maybe import Just, Nothing >>> Just(5).or_default_lazy(lambda: [print('call'), 10][-1]) 5 >>> Nothing.or_default_lazy(lambda: [print('call'), 10][-1]) call 10
- or_default(value)[source]#
Get the value from a
Just
, or return the given default value.>>> from cg_maybe import Just, Nothing >>> Just(5).or_default(10) 5 >>> Nothing.or_default(10) 10
- Parameters:
value (
TypeVar
(_Y
)) – The default value that will be returned for aNothing
.- Return type:
_T
- or_none()[source]#
Get the value from a
Just
, orNone
.>>> from cg_maybe import Just, Nothing >>> Just(5).or_none() 5 >>> Nothing.or_none() is None True
- Return type:
_T
- case_of(*, just, nothing)[source]#
A poor mans version of pattern matching.
>>> from cg_maybe import Just, Nothing >>> obj1, obj2 = object(), object() >>> on_just = lambda el: [print('just', el), obj1][1] >>> on_nothing = lambda: [print('nothing'), obj2][1] >>> Nothing.case_of(just=on_just, nothing=on_nothing) is obj2 nothing True >>> Just(5).case_of(just=on_just, nothing=on_nothing) is obj1 just 5 True
- Parameters:
- Return type:
_TT
- if_just(callback)[source]#
Call the given callback with the wrapped value if this value is a
Just
, otherwise do nothing.>>> from cg_maybe import Just, Nothing >>> printer = lambda el: print('call', el) >>> Nothing.if_just(printer) Nothing >>> Just(5).if_just(printer) call 5 Just(5)
- if_nothing(callback)[source]#
Call the given callback if this value is a
Nothing
, otherwise do nothing.>>> from cg_maybe import Just, Nothing >>> printer = lambda: print('call') >>> _ = Just(5).if_nothing(printer) >>> _ = Nothing.if_nothing(printer) call
- try_extract(make_exception)[source]#
Try to extract the value, raising an exception created by the given argument if the value is
Nothing
.>>> from cg_maybe import Just, Nothing >>> Just(5).try_extract(Exception) 5 >>> Nothing.try_extract(lambda: Exception()) Traceback (most recent call last): ... Exception >>> Nothing.try_extract(Exception()) Traceback (most recent call last): ... Exception
- attr(attr)[source]#
Get the given attribute from the value in the just and wrap the result in a just.
This means that
value.attr('attr')
is equal tovalue.map(lambda v: v.attr)
.
-
value: