Skip to content

Ops (utilities for working with multiple functions)

utilz.ops

Functional tools intended to be used with one or more functions and a single object.

Overview
function (s) description
do apply a function or method to an object
many applies multiple functions indepedently to a single input and returns an iterable; like an "inverse map"
compose combine multiple functions into one function sequence (mini-pipe)
iffy apply a function if a predicate function is true otherwise noop

compose(*funcs)

Compose multiple functions into a single function

Parameters:

Name Type Description Default
funcs any

any number of functions listed as separate args

required

Returns:

Name Type Description
callable

new composed function

Source code in utilz/ops.py
105
106
107
108
109
110
111
112
113
114
115
@curry
def compose(*funcs):
    """
    Compose multiple functions into a single function

    Args:
        funcs (any): any number of functions listed as separate args
    Returns:
        callable: new composed function
    """
    return compose_left(*funcs)

do(func, data, *args, **kwargs)

Apply a single function to data or call a method on data, while passing optional kwargs to that functinon or method

Parameters:

Name Type Description Default
func callable/str

callable or str method name

required
data any

object to apply function to

required
kwargs any

additional arguments to function or method

required

Returns:

Name Type Description
any

function or method evaluation

Source code in utilz/ops.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@curry
def do(func, data, *args, **kwargs):
    """
    Apply a single function to data or call a method on data, while passing optional
    kwargs to that functinon or method

    Args:
        func (callable/str): callable or str method name
        data (any): object to apply function to
        kwargs (any): additional arguments to function or method

    Returns:
        any: function or method evaluation
    """
    from operator import methodcaller as mc

    if isinstance(func, str):
        func = mc(func, *args, **kwargs)
    return func(data)

iffy(predicate_func, if_true, data)

Conditionally apply a function based on a predicate function. Useful to conditionally map a function to an interable when combined with mapcat

Parameters:

Name Type Description Default
conditional callable

callable function on which data will be evaluated.

required
if_true any, callable

Value or function to call if predicate_func

required
data any

previous pipe output

required

Returns:

Name Type Description
any

function evaluation or original data

Source code in utilz/ops.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@curry
def iffy(predicate_func: Callable, if_true: Union[Any, Callable], data: Any):
    """
    Conditionally apply a function based on a predicate function. Useful to
    conditionally map a function to an interable when combined with mapcat

    Args:
        conditional (callable): callable function on which data will be evaluated.
        Should return a boolean value
        if_true (any, callable, optional): Value or function to call if predicate_func
        is True
        data (any): previous pipe output

    Returns:
        any: function evaluation or original data
    """

    if not callable(predicate_func):
        raise TypeError("iffy requires a function that returns a boolean value")

    if predicate_func(data):
        if callable(if_true):
            return if_true(data)
        else:
            return if_true
    else:
        return data

many(funcs, data)

Takes an iterable of funcs and applies them each separately to data. Operates like the inverse of map(). Whereas map takes applies 1 func to multiple elements, many applies multiple funcs independently to 1 element. Returns a tuple the same length as funcs, containing the output of each function

Parameters:

Name Type Description Default
funcs iterable

iterable of functions to apply

required
data any

data to aplly functions to

required

Returns:

Name Type Description
list

separate function evaluations

Source code in utilz/ops.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def many(funcs, data):
    """
    Takes an iterable of funcs and applies them each separately to data. Operates like the inverse of map(). Whereas map takes applies 1 func to multiple elements, many applies multiple funcs independently to 1 element. Returns a tuple the same length as funcs, containing the output of each function


    Args:
        funcs (iterable): iterable of functions to apply
        data (any): data to aplly functions to


    Returns:
        list: separate function evaluations
    """

    if isinstance(data, (list, tuple)):
        raise TypeError(
            f"Expected a single input but receive {len(data)}. Use mapmany() to operate on an iterable"
        )
    if not isinstance(funcs, (list, tuple)) or len(funcs) <= 1:
        raise ValueError(
            f"many applies *multiple* function calls separately but only received a single function. Use do() to apply a single function or method."
        )

    return [do(f, data) for f in funcs]