Build partials and quick lambdas
When a pipeline stage needs a small function, writing out lambda (or
functools.partial) gets tedious. pipescript offers several progressively
terser ways to build one.
Currying with a trailing $
You can curry a function by supplying leading arguments and marking the rest
with $, much like functools.partial():
>>> from functools import reduce
>>> add_reducer = reduce(lambda x, y: x + y, $, $)
>>> add_reducer([1, 2, 3], 0)
6
Partial-call syntax ($)
To avoid writing a $ for every trailing argument, prefix the call itself
with $ and simply stop – pipescript fills in the rest, just like in
coconut:
>>> add_reducer = reduce$(lambda x, y: x + y)
>>> add_reducer([1, 2, 3], 0)
6
>>> add_reducer([[1, 2, 3], [4, 5, 6]], [])
[1, 2, 3, 4, 5, 6]
Because the induced partial keeps the original function’s argument defaults, you
can often omit trailing arguments entirely (reduce’s initializer here):
>>> add_reducer = reduce$(lambda x, y: x + y)
>>> add_reducer([1, 2, 3])
6
Higher-order macros (reduce[...])
Currying map, filter, and reduce with a function is so common that
pipescript provides bracketed macro forms. The function to curry with goes
between the brackets:
>>> add_reducer = reduce[lambda x, y: x + y]
>>> [1, 2, 3] |> add_reducer
6
Quick lambdas with f[...]
Writing lambda x, y: x + y is still verbose. The f macro turns a
placeholder expression into a lambda:
>>> add_reducer = reduce[f[$ + $]]
>>> [1, 2, 3] |> add_reducer
6
f works on its own, too, and honors named placeholders:
>>> f[$ + $](2, 3)
5
>>> f[$a*$b + $b*$c + $a*$c](2, 3, 4)
26
Omitting f inside higher-order macros
Inside a higher-order functional macro you can drop the f altogether and
write the placeholder expression directly:
# factorial: 1 * 2 * 3 * 4
>>> range(1, 5) |> reduce[$ * $]
24
# assemble a number from its decimal digits
>>> [2, 3, 4] |> reduce[10*$ + $]
234