Just#

class Just[source]#

The just part of the Maybe monad.

is_just: Literal[True] = True#
is_nothing: Literal[False] = False#
value: Final#
map(mapper)[source]#

Transform this just by applying mapper to its argument and wrapping the result in a new Just.

>>> from cg_maybe import Just, Nothing
>>> Just(5).map(lambda el: el * el)
Just(25)
>>> Nothing.map(lambda el: el * el)
Nothing
Parameters:

mapper (Callable[[TypeVar(_T, covariant=True)], TypeVar(_TT, covariant=True)]) – The function that will be called to map the value if this method is called on a Just.

Return type:

Just[_TT]

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
Parameters:
  • mapper (Callable[[TypeVar(_T, covariant=True)], TypeVar(_Y)]) – The function that will be called if this method is called on a Just.

  • default (TypeVar(_Z)) – The default value that will be called if this method is called on a Nothing.

Return type:

_Y

chain(chainer)[source]#

Transforms this with a function that returns a Maybe.

>>> 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
Parameters:

chainer (Callable[[TypeVar(_T, covariant=True)], Union[Just[TypeVar(_TT, covariant=True)], _Nothing[TypeVar(_TT, covariant=True)]]]) – The function that will be called if this method is called on Just. It should return a maybe.

Return type:

Just[_TT] | _Nothing[_TT]

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
Parameters:

chainer (Callable[[TypeVar(_T, covariant=True)], Optional[TypeVar(_TT, covariant=True)]]) – The function that will be called if this method is called on Just. It should return a Optional value.

Return type:

Just[_TT] | _Nothing[_TT]

alt(alternative)[source]#

Return the given alternative if called on a Nothing, 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)
Parameters:

alternative (Union[Just[TypeVar(_T, covariant=True)], _Nothing[TypeVar(_T, covariant=True)]]) – The value return if this method is called on a Nothing.

Return type:

Just[_T] | _Nothing[_T]

alt_lazy(maker)[source]#

Return the result of maker if called on a Nothing, 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)
Parameters:

maker (Callable[[], Union[Just[TypeVar(_Y)], _Nothing[TypeVar(_Y)]]]) – The function that will be called if this method is called on a Nothing.

Return type:

Just[_Y | _T] | _Nothing[_Y | _T]

unsafe_extract()[source]#

Get the value from a Just, or raise if called on a Nothing.

>>> 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
Parameters:

producer (Callable[[], TypeVar(_Y)]) – The function that will produce the default value for Nothing.

Return type:

_T

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 a Nothing.

Return type:

_T

or_none()[source]#

Get the value from a Just, or None.

>>> 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:
  • just (Callable[[TypeVar(_T, covariant=True)], TypeVar(_TT, covariant=True)]) – The function that will be called if this method is called on a Just.

  • nothing (Callable[[], TypeVar(_TT, covariant=True)]) – The function that will be called if this method is called on a Nothing.

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)
Parameters:

callback (Callable[[TypeVar(_T, covariant=True)], object]) – The function that will be called if this method is called on a Just. It will be provided the value of the just.

Return type:

Just[_T]

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
Parameters:

callback (Callable[[], object]) – The function that will be called if this method is called on a Nothing.

Return type:

Just[_T]

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
Parameters:

make_exception (Union[Callable[[], Exception], Exception]) – The function that will be called to create an exception that will be raised if called on a Nothing. If this value is an instance of BaseException it will be raised directly.

Return type:

_T

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 to value.map(lambda v: v.attr).

Parameters:

attr (str) – The attribute to get of the value of a Just.

Return type:

object

join()[source]#

Join a Just of a Maybe.

This is equal to maybe.chain(x => x).

Parameters:

self (Just[Just[_Y] | _Nothing[_Y]]) –

Return type:

Just[_Y] | _Nothing[_Y]

filter(pred)[source]#

Filter this maybe with a predicate.

Parameters:

pred (Callable[[TypeVar(_T, covariant=True)], bool]) – The predicate to filter the maybe with.

Returns:

Itself if pred returns True, otherwise Nothing.

Return type:

Just[_T] | _Nothing[_T]

eq(val)[source]#

Check if a value of a Just is equal to the given value.

Parameters:
  • val (TypeVar(_Y)) – The value to check this Maybe against.

  • self (Just[_Y]) –

Returns:

True if val equals the value of the Just, always False for a Nothing.

Return type:

bool

ne(val)[source]#

Check if a value of a Just is not equal to the given value.

Parameters:
  • val (TypeVar(_Y)) – The value to check this Maybe against.

  • self (Just[_Y]) –

Returns:

True if val does not equal the value of the Just, always False for a Nothing.

Return type:

bool

lt(val)[source]#

Check if a value of a Just is less than the given value.

Parameters:
  • val (SupportsLessThan[TypeVar(_T, covariant=True)]) – The value to check this Maybe against.

  • self (Just[SupportsLessThan[_T]]) –

Returns:

True if val is less than the value of the Just, always False for a Nothing.

Return type:

bool

le(val)[source]#

Check if a value of a Just is <= to the given value.

Parameters:
  • val (SupportsGreaterOrEqual[TypeVar(_T, covariant=True)]) – The value to check this Maybe against.

  • self (Just[SupportsGreaterOrEqual[_T]]) –

Returns:

True if val is less than or equal to the value of the Just, always False for a Nothing.

Return type:

bool

gt(val)[source]#

Check if a value of a Just is > to the given value.

Parameters:
  • val (SupportsLessThan[TypeVar(_T, covariant=True)]) – The value to check this Maybe against.

  • self (Just[SupportsLessThan[_T]]) –

Returns:

True if val is greater than to the value of the Just, always False for a Nothing.

Return type:

bool

ge(val)[source]#

Check if a value of a Just is >= to the given value.

Parameters:
  • val (SupportsGreaterOrEqual[TypeVar(_T, covariant=True)]) – The value to check this Maybe against.

  • self (Just[SupportsGreaterOrEqual[_T]]) –

Returns:

True if val is greater than or equal to the value of the Just, always False for a Nothing.

Return type:

bool