Higher order functions
|
Similar to python's reduce, but can be prematurely terminated with |
|
Use with |
|
returns a function that takes in a flipped order of args. |
|
Fold right. |
|
Returns a curried version of the function. |
|
Thread first, passes |
|
Thread last, passes |
|
takes a function |
|
returns a composed function that takes a variable number of args, and applies it to |
- functionali.reduce(fn, iterable, initial=None)
Similar to python’s reduce, but can be prematurely terminated with
reduced. Works with dictionaries too.Usage:
>>> # Reducing over dictionaries. >>> def inc_value(result, kv_pair): k = kv_pair[0] v = kv_pair[1] return result[k]= v+1 >>> reduce(inc_value, {"a":1,"b":2}, {}) {'a': 2, 'b': 3}
>>> #premature termination with reduced >>> def inc_while_odd(result, element): if element%2==0: return reduced(result) else: result.append(element+1) return result >>> reduce(inc_while_odd, [1,3,5,6,7,8],[]) [2, 4, 6] # increments uptil 5 (third element) and prematurely terminates.
- functionali.reduced(x)
Use with
functionali.reduceto prematurely terminatereducewith the value ofx.Usage:
>>> reduce(lambda acc, el: reduced("!"), [1,3,4]) "!" # reduce is prematurely terminated and returns a value of "!"
- functionali.flip(fn: Callable) Callable
returns a function that takes in a flipped order of args. Usage:
>>> f = lambda a,b : a-b >>> f(1,3) -2 >>> f(3,1) 2 >>> flipped_f = flip(f) >>> flipped_f(3,1) -2 >>> flipped_f(1,3) 2
Added in version: 0.1.0
- functionali.foldr(fn: Callable, iterable: Iterable, initial: Optional[Any] = None) Any
Fold right. Stack safe implementation
Added in version: 0.1.0
- functionali.curry(fn: Callable) Callable
Returns a curried version of the function.
>>> def fn(arg1, arg2, arg3): # test function return [arg1, arg2, arg3] >>> curried_fn = curry(fn) >>> curried_fn(1)(2)(3) [1, 2, 3]
Added in version: 0.1.0
- functionali.threadf(arg: Any, forms: Iterable[Union[Callable, Iterable]]) Any
Thread first, passes
argas the first argument to the first function informsand passes the result as the first argument to the second form and so on.see also
threadl.>>> from functionali import identity >>> from operator import add, sub, mul >>> threadf(5, [identity]) >>> 5
>>> threadf(5, [identity, [add, 2]]) >>> 7
>>> threadf(5, [[sub, 2]]) >>> 3 # threadf(5, [[sub, 2]]) -> sub(5, 2) -> 5-2 -> 3
>>> # combining multiple functions >>> threadf(5, [identity, (add, 1), (sub, 1), (mul, 3)]) 15
- functionali.threadl(arg: Any, forms: Iterable[Union[Callable, Iterable]]) Any
Thread last, passes
argas the last argument to the first function informsand passes the result as the last argument to the second form and so on.see also
threadf.>>> from functionali import identity >>> from operator import add, sub, mul >>> threadl(5, [identity]) >>> 5
>>> threadl(5, [identity, [add, 2]]) >>> 7
>>> threadl(5, [[sub, 2]]) >>> -3 # threadl(5, [[sub, 2]]) -> sub(2, 5) -> 2-5 -> -3
>>> # combining multiple functions >>> threadl(5, [identity, (add, 1), (sub, 1), (mul, 3)]) -15
- functionali.trampoline(fn: Callable, *args: Any)
takes a function
fnand calls if with*args. iffnreturns a function, calls the function until a function is not returned i.e. the base case is reached. functionfnmust return a function in its recursive case. Useful for optimizing tail recursive functions or mutual recursions.>>> def fact(x, curr=1, acc=1): >>> if curr == x: >>> return curr*acc >>> else: >>> return lambda: fact(x, curr+1, acc*curr) >>> trampoline(fact, 3) == 6 >>> trampoline(fact, 100000000000) # does not raise RecursionError
- functionali.comp(*fns: Callable)
returns a composed function that takes a variable number of args, and applies it to
fnspassed from right to left.Added in version: 0.1.2