Utils¶
-
pymonet.utils.
identity
(value: T) → T¶ Return first argument.
- Parameters
value (Any) –
- Returns
- Return type
Any
-
pymonet.utils.
increase
(value: int) → int¶ Return increased by 1 argument.
- Parameters
value (Int) –
- Returns
- Return type
Int
-
pymonet.utils.
eq
(*args)¶
-
pymonet.utils.
curried_map
(*args)¶
-
pymonet.utils.
curried_filter
(*args)¶
-
pymonet.utils.
compose
(value, *functions)¶ Perform right-to-left function composition.
- Parameters
value (Any) – argument of first applied function
functions (List[Function]) – list of functions to applied from right-to-left
- Returns
result of all functions
- Return type
Any
from pymonet.utils import \
increase,\
compose,\
curried_map as map,\
curried_filter as filter
compose(
list(range(10)),
map(increase),
filter(is_odd)
)
#[1, 3, 5, 7, 9]
-
pymonet.utils.
pipe
(value, *functions)¶ Perform left-to-right function composition.
- Parameters
value (Any) – argument of first applied function
functions (List[Function]) – list of functions to applied from left-to-right
- Returns
result of all functions
- Return type
Any
from pymonet.utils import increase, pipe
pipe(42, increase, lambda value: value * 2)
#86
-
pymonet.utils.
curry
(x, args_count=None)¶ In mathematics and computer science, currying is the technique of translating the evaluation of a function. It that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions. each with a single argument.
from pymonet.utils import curry
@curry
def fn(arg1, arg2, arg3):
return arg1 + arg2 + arg3
fn(1)(2)(3) # 6
fn(1, 2)(3) # 6
fn(1)(2, 3) # 6
fn(1, 2, 3) # 6
-
pymonet.utils.
cond
(condition_list: List[Tuple[Callable[[T], bool], Callable]])¶ Function for return function depended on first function argument cond get list of two-item tuples, first is condition_function, second is execute_function. Returns this execute_function witch first condition_function return truly value.
- Parameters
condition_list (List[(Function, Function)]) – list of two-item tuples (condition_function, execute_function)
- Returns
Returns this execute_function witch first condition_function return truly value
- Return type
Function
from pymonet.utils import cond
fn = cond([
(lambda arg: arg == 0, lambda: 'first'),
(lambda arg: arg == 1, lambda: 'second'),
(lambda arg: arg == 2, lambda: 'third').
])
fn(1) # second
# lambda arg: arg == 2 will not be call
-
pymonet.utils.
memoize
(fn: Callable, key=<function curry.<locals>.fn>) → Callable¶ Create a new function that, when invoked, caches the result of calling fn for a given argument set and returns the result. Subsequent calls to the memoized fn with the same argument set will not result in an additional call to fn; instead, the cached result for that set of arguments will be returned.
- Parameters
fn (Function(A) -> B) – function to invoke
key (Function(A, A) -> Boolean) – function to decide if result should be taken from cache
- Returns
new function invoking old one
- Return type
Function(A) -> B
from pymonet.utils import memoize, eq
def fn(arg):
print('fn flag')
return arg + 1
memoized_fn = memoize(fn)
memoized_fn(42) # 43
# fn flag
memoized_fn(42) # 43
# print to called
memoized_fn(43) # 44
# fn flag