4 ms·
First off, well done. My big issue with method chaining is exactly the one of requiring the line-continuation-escape, or enclosing the whole thing in parens. Y
by andreareina 9y ago
First off, well done.
My big issue with method chaining is exactly the one of requiring the line-continuation-escape, or enclosing the whole thing in parens. You also need to wrap the initial value, then possibly unwrap it at the end, and you're limited to the methods you define (mind you, this seems like a pretty comprehensive list).
For this reason I decided to emulate clojure's threading macros instead, as a pair of functions:
pipe_{first,last}(
init_val,
some_unary_callable, # so far so familiar
(some_n_ary_callable, *args), # puts the piped value first or last, depending on {first/last}
...
)
Unfinished is the version that makes `(callable, ...args..., _, ...more_args...)` work, is that what you mean by the _ lambda operator?
- senand 9y agoAre you aware of the thread_first and thread_last functions from toolz?
- andreareina 9y agoI wasn't thanks for the pointer!
- entilzha 9y agoNot OP, but author. Line continuation is pretty annoying. Adding custom methods wouldn't be hard (https://github.com/EntilZha/PyFunctional/issues/113 https://github.com/EntilZha/PyFunctional/issues/113), I just don't have time right now to add it. With respect to _, this would make these two equivalent: a = lambda x: x + 1 b = _ + 1 a(1) == b(1) Is there an expanded example I could look at for the pipe stuff, I haven't used clojure before