3 ms·
Wrote something similar in Python, even called it the same! def pipe(init_val, *funcs): """Apply `init_val` to the composition of `funcs` pipe(
by andreareina 9y ago
Wrote something similar in Python, even called it the same!
def pipe(init_val, *funcs):
"""Apply `init_val` to the composition of `funcs`
pipe(init, func1, func2, ..., funcn) -> funcn(...(func2(func1(init_val))))
"""
return functools.reduce(lambda val, func: func(val), funcs, init_val)
Check out clojure's threading macros[1] which extends that to taking forms, where the current value is spliced in as the first or last argument. e.g., with ->> (thread-last):
(->> '(3 4 5) (map 1+) (reduce +)) ;; 15
Fits the bill for short functions, and you can still have lambdas for when you really need them.
[1] https://clojure.org/guides/threading_macros https://clojure.org/guides/threading_macros
- huntie 9y ago>threading macros Wow that's pretty awesome. I'd been annoyed with needing to use lambdas for this type of thing. I especially like the as-> macro. I need to take the time to explore Scheme macros to see what other useful abstractions they can provide.