3 ms·
I don't know if result = (df .pipe(fun1, arg1=1) .pipe(fun2, arg2=2) ) is much less readable than result <- df |> fun1(., arg1=1)
by nxpnsv 1y ago
I don't know if
result = (df
.pipe(fun1, arg1=1)
.pipe(fun2, arg2=2)
)
is much less readable than
result <- df |>
fun1(., arg1=1) |>
fun2(., arg2=2)
but I guess the R thing also works beyond dataframes which is pretty cool
- mb7733 1y agoI haven't used R in forever, but is your `.` placeholder actually necessary? From my recollection of pipe operator the value being pipe piped is automatically as the first argument to the next function. That may have been a different implementation of a pipe operator though.
- nxpnsv 1y agoProbably not, I didn’t use R much during the last decade …
- itishappy 1y agoThe pipe operator uses what comes before as the first argument of the function. This means in R it would be: result <- df |> fun1(arg1=1) |> fun2(arg2=2) Python doesn't have a pipe operator, but if it did it would have similar syntax: result = df |> fun1(arg1=1) |> fun2(arg2=2) In existing Python, this might look something like: result = pipe(df, [ (fun1, 1), (fun2, 2) ]) (Implementing `pipe` would be fun, but I'll leave it as an exercise for the reader.) Edit: Realized my last example won't work with named arguments like you've given. You'd need a function for that, which start looking awful similar to what you've written: result = pipe(df, [ step(fun1, arg1=1), step(fun2, arg2=2) ])
- Izkata 1y agoPython supports a syntax like your first example by implementing the appropriate magic method for the desired operator and starting the chain with that special object. For example, using just a single pipe: https://flexiple.com/python/python-pipeline-operator https://flexiple.com/python/python-pipeline-operator The functions with extra arguments could be curried, or done ad-hoc like lambda v: fun1(v, arg1=1)
- superbatfish 1y ago>Implementing `pipe` would be fun, but I'll leave it as an exercise for the reader. I like exercise: https://gist.github.com/stuarteberg/6bcbe3feb7fba4dc2574a989f196ee11 https://gist.github.com/stuarteberg/6bcbe3feb7fba4dc2574a989...
- nxpnsv 1y agoNeat!
- nxpnsv 1y agoThanks!
- lsaferite 1y agoI find this (hypothetical) syntax *very* elegant. result = df |> fun1(arg1=1) |> fun2(arg2=2)