5 ms·
Exactly what yoavm said. The pipelining that shell languages do allows for more complex and more arbitrary data transformations. I feel that doing this in Pytho
by huntie 9y ago
Exactly what yoavm said. The pipelining that shell languages do allows for more complex and more arbitrary data transformations. I feel that doing this in Python would be difficult, but I'm not very familiar with it. In scheme you can do something like this:
(define (pipe . exprs)
(if (null? (cdr exprs))
(car exprs)
(apply pipe (cons ((cadr expr) (car expr))
(cddr expr)))))
(pipe '(3 4 5) (lambda (x) (map 1+ x))) ;; '(4 5 6)
- andreareina 9y agoWrote 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.