composevs. thread_*andpipe
composeis essentially a function linker (∘). The main goal is to combine various functions into reusable blocks . The order of applications is reversed in the order of arguments, so compose(f, g, h)(x)is f(g(h(x)))(just like (f ∘ g) (x) is f (g (x))).
thread_* pipe . , . , , pipe(x, f, g, h) - h(g(f(x))).
compose vs thread_*.
compose , thread_*. currying compose .
, thread_ , :
thread_last(
range(10),
(map, lambda x: x + 1),
(filter, lambda x: x % 2 == 0)
)
compose :
pipe(
range(10),
lambda xs: map(lambda x: x + 1, xs),
lambda xs: filter(lambda x: x % 2 == 0, xs)
)
from toolz import curried
pipe(
range(10),
curried.map(lambda x: x + 1),
curried.filter(lambda x: x % 2 == 0)
)
thread_first vs. thread_last.
thread_first piped .
thread_last piped .
>>> from operator import pow
>>> thread_last(3, (pow, 2))
8
>>> thread_first(3, (pow, 2))
9
( ) , functools.partial/toolz.curry lambda, .
, , map functools.reduce, thread_last . , compose(h, g, f), def fgh(x) pipe(x, f, g, h). .