xotl.tools.fp.tools – High-level pure function tools

Tools for working with functions in a more “pure” way.

class xotl.tools.fp.tools.compose(*funcs)[source]

Composition of several functions.

Functions are composed right to left. A composition of zero functions gives back the identity() function.

Rules must be fulfilled (those inner all):

>>> x = 15
>>> f, g, h = x.__add__, x.__mul__, x.__xor__
>>> all((compose() is identity,
...
...      # identity functions are optimized
...      compose(identity, f, identity) is f,
...
...      compose(f) is f,
...      compose(g, f)(x) == g(f(x)),
...      compose(h, g, f)(x) == h(g(f(x)))))
True

If any “intermediate” function returns an instance of:

  • pos_args: it’s expanded as variable positional arguments to the next function.
  • kw_args: it’s expanded as variable keyword arguments to the next function.
  • full_args: it’s expanded as variable positional and keyword arguments to the next function.

The expected usage of these is not to have function return those types directly, but to use them when composing functions that return tuples and expect tuples.

xotl.tools.fp.tools.identity(arg)[source]

Returns its argument unaltered.

xotl.tools.fp.tools.fst(pair, strict=True)[source]

Return the first element of pair.

If strict is True, pair needs to unpack to exactly two values. If strict is False this is the same as pair[0].

Note

This is an idiomatic function intended for using in compositions or as the argument or high-level functions. Don’t use it in your code as a replacement of x[0].

New in version 1.8.5.

xotl.tools.fp.tools.snd(pair, strict=True)[source]

Return the second element of pair.

If strict is True, pair needs to unpack to exactly two values. If strict is False this is the same as pair[1].

Note

This is an idiomatic function intended for using in compositions or as the argument or high-level functions. Don’t use it in your code as a replacement of x[1].

New in version 1.8.5.

xotl.tools.fp.tools.constant(value)[source]

Return a function that always return a constant value.

class xotl.tools.fp.tools.pos_args[source]

Mark variable number positional arguments (see full_args).

class xotl.tools.fp.tools.kw_args[source]

Mark variable number keyword arguments (see full_args).

class xotl.tools.fp.tools.full_args[source]

Mark variable number arguments for composition.

Pair containing positional and keyword (args, kwds) arguments.

In standard functional composition, the result of a function is considered a single value to be use as the next function argument. You can override this behaviour returning one instance of pos_args, kw_args, or this class; in order to provide multiple arguments to the next call.

Since types are callable, you may use it directly in compose() instead of changing your functions to returns the instance of one of these classes:

>>> def join_args(*args):
...     return ' -- '.join(str(arg) for arg in args)

>>> compose(join_args, pos_args, list, range)(2)
'0 -- 1'

# Without 'pos_args', it prints the list
>>> compose(join_args, list, range)(2)
'[0, 1]'