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