cozy.functools_ext

Module Contents

Functions

preorder_mapfold(→ tuple[any, T])

Simultaneously maps and folds over a nested Python datastructure in preorder traversal order. The datastructure may consist of arbitrarily nested lists, tuples, dictionaries and sets. Note that for dictionaries, both keys and values will be traversed.

preorder_fold(→ U)

Folds over a Python datastructure in preorder traversal. The datastructure may consist of arbitrarily nested lists, tuples, dictionaries and sets. Note that for dictionaries, both keys and values will be traversed.

fmap(→ any)

Maps a Python datastructure. The datastructure may consist of arbitrarily nested lists, tuples, dictionaries and sets.

compose(→ collections.abc.Callable[[Ellipsis], C])

Composes two functions, f and g, to create a new function h(*a, **kw) = f(g(*a, **kw))

Attributes

T

U

V

B

C

cozy.functools_ext.T
cozy.functools_ext.U
cozy.functools_ext.V
cozy.functools_ext.B
cozy.functools_ext.C
cozy.functools_ext.preorder_mapfold(val0: any, f: collections.abc.Callable[[any, T], tuple[any, T]], accum0: T, sort=True) tuple[any, T]

Simultaneously maps and folds over a nested Python datastructure in preorder traversal order. The datastructure may consist of arbitrarily nested lists, tuples, dictionaries and sets. Note that for dictionaries, both keys and values will be traversed.

Parameters:
  • val0 (any) – The datastructure to traverse.

  • f (Callable[[any, T], tuple[any, T]]) – This function takes as input a value inside the datastructure, the accumulated value and should return a mapped value and newly accumulated value.

  • accum0 (T) – Initial accumulation parameter.

Returns:

The mapped datastructure and final accumulated value.

Return type:

tuple[any, T]

cozy.functools_ext.preorder_fold(val0: any, f: collections.abc.Callable[[any, U], U], accum0: U) U

Folds over a Python datastructure in preorder traversal. The datastructure may consist of arbitrarily nested lists, tuples, dictionaries and sets. Note that for dictionaries, both keys and values will be traversed.

Parameters:
  • val0 (any) – The datastructure to traverse.

  • f (Callable[[any, U], U]) – This function takes as input the value inside the datastructure, the accumulated value and should return a new accumulated value.

  • accum0 (U) – Initial accumulation parameter.

Returns:

The final accumulated value.

Return type:

U

cozy.functools_ext.fmap(val0: any, f: collections.abc.Callable[[any], any]) any

Maps a Python datastructure. The datastructure may consist of arbitrarily nested lists, tuples, dictionaries and sets.

Parameters:

val0 (any) – The datastructure to map. Note that for dictionaries, both keys and values will be mapped.

Returns:

The mapped datastructure.

Return type:

any

cozy.functools_ext.compose(f: collections.abc.Callable[[B], C], g: collections.abc.Callable[[Ellipsis], B]) collections.abc.Callable[[Ellipsis], C]

Composes two functions, f and g, to create a new function h(*a, **kw) = f(g(*a, **kw))

Parameters:
  • f (Callable[[B], C]) – The first function to compose.

  • g (Callable[[...], B]) – The second function to compose.

Returns:

A newly composed function which takes in an arbitrary number of arguments and keyword arguments, and returns a C.

Return type:

Callable[[…], C]