4 ms·
Shoutout to the pipe operator in R. The code equivalent of "and then." It helps to unnest functions and put each action on one line. I know R is more for stats
by snitzr 2y ago
Shoutout to the pipe operator in R. The code equivalent of "and then." It helps to unnest functions and put each action on one line. I know R is more for stats and data, but I just think it's neat.
- stared 2y agoYes, dplyr pipes are wonderful. Also, for the same reason, I find JavaScript list comprehensions cleaner than those in Python - as in the former it is possible to chain maps and filters. Also, now there is a new pipe syntax in SQL, that adds a lot to readability.
- flobosg 2y agoI just wanted to point out that, apart from dplyr/magrittr, R introduced a native pipe operator (|>) a few years ago.
- memhole 2y agoFor anyone interested in this as design, it’s called method chaining.
- closed 2y agoI think piping and method chaining are a little bit different. Piping generally chains functions, by passing the result of one call into the next (eg result is first argument to the next). Method chaining, like in Python, can't do this via syntax. Methods live on an object. Pipes work on any function, not just an object's methods (which can only chain to other object methods, not any function whose eg first argument can take that object). For example, if you access Polars.DataFrame.style it returns a great_tables.GT object. But in a piping world, we wouldn't have had to add a style property that just calls GT() on the data. With a pipe, people would just be able to pipe their DataFrame to GT().
- memhole 2y agoGood to know. I assumed it was all done via objects or things like objects. So is piping more functional programming?
- closed 2y agoI think it's often a syntax convenience. For example, Polars and Pandas both have DataFrame.pipe(...) methods, that create the same effect. But it's a bit cumbersome to write. Here's a comparison: * Method chaining: `df.pipe(f1, a=1, b=2).pipe(f2, c=1)` * Pipe syntax: `df |> f1(a=1, b=2) |> f2(c=1)`
- memhole 2y agoOk, that’s helpful. Thanks!
- __mharrison__ 2y agoI wrote a book dedicated to writing Pandas code in this style, Effective Pandas 2. I've seen many who complain about this style of coding, but once they try it, they are sold. I love reading reviews about how adopting this made their code easier to write, debug, read, and collaborate.