Skip to content

STD Types

This part of the documentation shows some wrappers around Python's builtin types.

monad_std.std_types.MDict

Bases: Generic[K, V], Dict[K, V]

Source code in monad_std/std_types/pydict.py
class MDict(t.Generic[K, V], t.Dict[K, V]):
    def get(self, key: K) -> Option[V]:
        try:
            return Option.some(super().__getitem__(key))
        except KeyError:
            return Option.none()

    def popitem(self) -> Option[MTuple]:
        try:
            return Option.some(MTuple(super().popitem()))
        except KeyError:
            return Option.none()

    def pop(self, key: K) -> Option[V]:
        try:
            return Option.some(super().pop(key))
        except KeyError:
            return Option.none()

get(key)

Get the value by the key.

Parameters:

Name Type Description Default
key K

The key to the value you want.

required

Examples:

1
2
3
x = MDict({'a': 1, 'b': 2, 'c': 3})
assert x.get('b') == Option.some(2)
assert x.get('d') == Option.none()
Source code in monad_std/std_types/pydict.py
def get(self, key: K) -> Option[V]:
    try:
        return Option.some(super().__getitem__(key))
    except KeyError:
        return Option.none()

pop(key)

Pop a value from the dict.

Parameters:

Name Type Description Default
key K

The key to pop.

required

Returns:

Type Description
Option[V]

The value under the key

Examples:

1
2
3
4
x = MDict({'a': 1})
assert x.pop('a') == Option.some(1)
assert x.pop('b') == Option.none()
assert x.pop('a') == Option.none()
Source code in monad_std/std_types/pydict.py
def pop(self, key: K) -> Option[V]:
    try:
        return Option.some(super().pop(key))
    except KeyError:
        return Option.none()

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order.

Returns:

Type Description
Option[MTuple[K, V]]

An optional key-value pair.

Examples:

1
2
3
x = MDict({'a': 1})
assert x.popitem() == Option.some(('a', 1))
assert x.popitem() == Option.none()
Source code in monad_std/std_types/pydict.py
def popitem(self) -> Option[MTuple]:
    try:
        return Option.some(MTuple(super().popitem()))
    except KeyError:
        return Option.none()

monad_std.std_types.MList

Bases: Generic[KT], List[KT]

Source code in monad_std/std_types/pylist.py
class MList(t.Generic[KT], t.List[KT]):
    def index(self, *args, **kwargs) -> Option[int]:
        try:
            return Option.some(super().index(*args, **kwargs))
        except ValueError:
            return Option.none()

    def get(self, index: t.SupportsIndex) -> Option[KT]:
        try:
            return Option.some(self.__getitem__(index))
        except IndexError:
            return Option.none()

    def pop(self, *args, **kwargs) -> Option[KT]:
        try:
            return Option.some(super().pop(*args, **kwargs))
        except (IndexError, AssertionError):
            return Option.none()

get(index)

Get an item from the list.

Returns:

Type Description
Option[KT]

The value at the given index, or Option::None if it's out of range.

Examples:

1
2
3
x = MList([1, 2, 3, 4, 5])
assert x.get(2) == Option.some(3)
assert x.get(10) == Option.none()
Source code in monad_std/std_types/pylist.py
def get(self, index: t.SupportsIndex) -> Option[KT]:
    try:
        return Option.some(self.__getitem__(index))
    except IndexError:
        return Option.none()

index(*args, **kwargs)

Return first index of value.

Actual Method Signature

1
2
3
def index(self, __value: KT,
    __start: SupportsIndex = ...,
    __stop: SupportsIndex = ...) -> Option[int]: ...

Parameters:

Name Type Description Default
__value KT

Value to search for.

required
__start SupportsIndex

Start index.

...
__stop SupportsIndex

Stop index.

...

Returns:

Type Description
Option[int]

First index of the value, or Option::None if there's no such item.

Examples:

1
2
3
x = MList([1, 2, 3, 4, 5])
assert x.index(2) == Option.some(1)
assert x.index(0) == Option.none()
Source code in monad_std/std_types/pylist.py
def index(self, *args, **kwargs) -> Option[int]:
    try:
        return Option.some(super().index(*args, **kwargs))
    except ValueError:
        return Option.none()

pop(*args, **kwargs)

Pop a value from the list.

Actual Method Signature

def pop(self, __index: SupportsIndex = ...) -> Option[KT]: ...

Parameters:

Name Type Description Default
__index SupportsIndex

The index to pop. Optional

...

Returns:

Type Description
Option[KT]

The last value or the value at the given index, or None if it's out of range.

Examples:

1
2
3
4
5
x = MList([1, 2, 3, 4, 5])
assert x.pop() == Option.some(5)
assert x.pop(1) == Option.some(2)
x = MList()
assert x.pop() == Option.none()
Source code in monad_std/std_types/pylist.py
def pop(self, *args, **kwargs) -> Option[KT]:
    try:
        return Option.some(super().pop(*args, **kwargs))
    except (IndexError, AssertionError):
        return Option.none()

monad_std.std_types.MSet

Bases: Generic[K], Set[K]

Source code in monad_std/std_types/pyset.py
class MSet(t.Generic[K], t.Set[K]):
    def pop(self) -> Option[K]:
        try:
            return Option.some(super().pop())
        except KeyError:
            return Option.none()

pop()

Remove and return an arbitrary set element.

Returns:

Type Description
Option[K]

The popped element.

Examples:

1
2
3
4
5
x = MSet([1, 'd'])
p1 = x.pop()
assert p1 == Option.some(1) or p1 == Option.some('d')
x.pop()
assert x.pop() == Option.none()
Source code in monad_std/std_types/pyset.py
def pop(self) -> Option[K]:
    try:
        return Option.some(super().pop())
    except KeyError:
        return Option.none()

monad_std.std_types.MTuple

Bases: Tuple

Source code in monad_std/std_types/pytuple.py
class MTuple(t.Tuple):
    def index(self, *args, **kwargs) -> Option[int]:
        try:
            return Option.some(super().index(*args, **kwargs))
        except ValueError:
            return Option.none()

    def get(self, index: int) -> Option[t.Any]:
        try:
            return Option.some(self.__getitem__(index))
        except IndexError:
            return Option.none()

get(index)

Get an item from the tuple.

Returns:

Type Description
Option[Any]

The value at the given index, or Option::None if it's out of range.

Source code in monad_std/std_types/pytuple.py
def get(self, index: int) -> Option[t.Any]:
    try:
        return Option.some(self.__getitem__(index))
    except IndexError:
        return Option.none()

index(*args, **kwargs)

Return first index of value.

Actual Method Signature

1
2
3
def index(self, __value: Any,
    __start: SupportsIndex = ...,
    __stop: SupportsIndex = ...) -> Option[int]: ...

Parameters:

Name Type Description Default
__value Any

Value to search for.

required
__start SupportsIndex

Start index.

...
__stop SupportsIndex

Stop index.

...

Returns:

Type Description
int

First index of the value, or Option::None if there's no such item.

Source code in monad_std/std_types/pytuple.py
def index(self, *args, **kwargs) -> Option[int]:
    try:
        return Option.some(super().index(*args, **kwargs))
    except ValueError:
        return Option.none()