I would just use any idiomatic Python tools available , such as list comprehension, as others have pointed out, rather than trying to pretend that you are writing Haskell, but if you really need to, you can use the combinator function compose
even in Python:
def compose(*args):
ret = identity
for f in reversed(args):
ret = compose2(f, ret)
return ret
def identity(x): return x
def compose2(f, g): return lambda x: f(g(x))
which you can use as follows:
from functools import partial
compose(partial(map, lambda x: x**2), partial(filter, lambda x: x % 2 == 0))(range(1, 21))
, , :
>>> compose(partial(map, lambda x: x**2), partial(filter, lambda x: x % 2 == 0))(range(1, 21))
[4, 16, 36, 64, 100, 144, 196, 256, 324, 400]
... , , Python , currying infix, , ( ) Haskell, .
$
:, Python - Haskell , Python, , .
$
Haskell:
zipWith ($) [(3*), (4+), (5-)] [1,2,3]
... Python () apply
"combinator", , :
>>> list(starmap(apply, zip([lambda x: 3 * x, lambda x: 4 + x, lambda x: 5 - x], map(lambda x: [x], [1, 2, 3]))))
[3, 6, 2]
- , Python:
- laziness , , , "" starmap
list()
"" ; - apply
(a -> b) -> a -> b
, (a1 -> a2 -> ... -> aN -> b) -> (a1, a2, ..., aN) -> b
, []
starmap
map
; ; - lambda , Guido lambdas,
map
, reduce
..;