Live data from Hacker News

R adds native pipe and lambda syntax

developer.r-project.org

131–140 of 150 posts

Re: R adds native pipe and lambda syntax

#131

That backslash syntax is pretty funky, but then again all of R is a little funky. Very nice addition to the language though!

Borrowed from Haskell. (Which of course does not make it unfunky.) https://wiki.haskell.org/Anonymous_function

In R all functions are anonymous functions. Functions are created anonymously and then (usually, but not always) assigned to a variable using the assignment operator.

Re: R adds native pipe and lambda syntax

#132
post #89

Question by someone who is ignorant but interested in functional programming: what is the closest equivalent to these functions in Python? (Or correct me if I'm asking the wrong question). I used to love using lambdas in Python, along with map/reduce/filter but for whatever reason the Python community has turned against it. Map and filter can now be nicely done with list comprehensions, although I still haven't found…

> Question by someone who is ignorant but interested in functional programming: what is the closest equivalent to these functions in Python? The equivalent to the function of the first is python’s lambda syntax, there's no simple syntax providing the same function as the pipe.

A pipe merely "pipes" the output of one function as an input to another. For example, | in bash. In Python this can be done the trivial way (by composing) or by using decorators.

Re: R adds native pipe and lambda syntax

#133
post #98
post #96

Earlier quoted context omitted.

As someone who is now bouncing back & forth between Python and R on a weekly basis, I've been surprised (after making fun of R sometimes) how much I miss the piping when I leave R for Python. Pandas seems so inflexible by comparison, so nitpicky for little gain. I've been surprised again and again how much dplyr supports near-effortless fluency and productivity. Never really thought I'd be writing that in a public fo…

I'm not an R user, but you should try Julia for data analysis. It seems as flexibility (maybe more) than R, while also having blazing performance. I do like Pandas concept of row indices, which I know Julia (and I believe, R) lack.

You should try using R; the real world performance is actually better than Julia in every way that matters. Yes, Julia has a better data frame object; that's why we use data.tables().

Also Wes probably got the idea for row indeces from R.

Re: R adds native pipe and lambda syntax

#134
post #56

Earlier quoted context omitted.

I know R because that's what we used at my first company. I would love to switch to Python/Pandas but I'm comfortable with R and it does everything I need it to with one exception over ten years of heavy use. Python is wonderful but the cognitive load for switching in industry and academia without a clear cost benefit isn't worth it to most people I know in my shoes. I encourage new coders to learn Python but discoun…

I made the switch years ago and there is lots that python does better. I really, really wish for a perfect port of dplyr and ggplot2. Those are what I truly miss, everything else I'm pretty happy with.

plotnine isn't a perfect port of ggplot2, but it's pretty close. https://plotnine.readthedocs.io/en/stable/

Re: R adds native pipe and lambda syntax

#135
post #124
post #98

Earlier quoted context omitted.

I'm not an R user, but you should try Julia for data analysis. It seems as flexibility (maybe more) than R, while also having blazing performance. I do like Pandas concept of row indices, which I know Julia (and I believe, R) lack.

I have used Julia a bit and really enjoyed it. The only reason I do not use it for work is lack of libraries. I know I could 'be the change I want to see in the world' and contribute, but given the pace of things at work I cannot fit that in on the company dime at this time...

Could you describe what kind of libraries you found lacking in Julia? I did get a feeling that lots of long-tail stuff was missing, when I was looking through Julia packages some time ago, but only in a vague "this doesn't seem that exhaustive" sense. Knowing what specifically has been found lacking would be useful.

Re: R adds native pipe and lambda syntax

#136

Earlier quoted context omitted.

The use of "." as an argument is actually probably one of my most common wtf's with pipes in general. I tend to use it a lot if I'm just piping a vector to base functions (gsub/grep have x as their third argument. This syntax looks like it makes that a little harder, but the new error messages are going to make everything so much better that I'm totally fine with it.

It is particularly infuriating in R, because lm(y ~ ., data = my_dataframe) already means "regress the variable y on all other columns in `my_dataframe`." For big, interactive regresions, it's really natural to write my_original_dataframe %>% do_a_bunch_of_tranformations() %>% select(...) %>% # Pull out just the columns you want lm(y ~ ., data = .) and god knows how that last line is going to be interpreted. So disam…

The zfit package is intended to address this issue, with the zlm() and comparable functions that are very thin wrappers around lm() and friends. The ony thing they do is flip the argument order so the data comes first, making exactly this use case much simpler. So you can do:

    cars %>% zlm(dist ~ speed)
(or now)

    cars |> zlm(dist ~ speed)
https://github.com/torfason/zfit

Re: R adds native pipe and lambda syntax

#138

nitpicking, but this is not a lambda. it's an anonymous function.

Care to elaborate?

A lambda, at least as understood in a functional programming context, is pure.

Whereas what is proposed here is simply syntactic sugar for creating an anonymous function; from the little that is said in the announcement there is no reason to think this syntax would provide any guarantees that state changes due to lexical scoping won't affect the function's output.

Re: R adds native pipe and lambda syntax

#139

Earlier quoted context omitted.

I'm not sure what specific "functions" you're talking about, but Python generally encourages a procedural style of programming as opposed to functional. The rationale is that functional code can be really difficult to read if you aren't already familiar with the idioms and terminology, whereas it's pretty easy to mentally parse and understand a `for` loop. So in that sense, list comprehensions are about as far as Pyt…

Having read plenty of python numerical code, I'm not sure "easy to parse and understand" is what exactly comes to mind.

Most of the problem comes from Pandas which is of course R-inspired.

Re: R adds native pipe and lambda syntax

#140

Earlier quoted context omitted.

> Question by someone who is ignorant but interested in functional programming: what is the closest equivalent to these functions in Python? The equivalent to the function of the first is python’s lambda syntax, there's no simple syntax providing the same function as the pipe.

A pipe merely "pipes" the output of one function as an input to another. For example, | in bash. In Python this can be done the trivial way (by composing) or by using decorators.

Yes, it can be done the trivial way in most languages. For deep nestung, that's ugly and awkward, which is why some languages have piping/composition operators [or threading macros] (sometimes more than one). Python has no close equivalent of a piping operator or threading macro (decorators don't seem helpful at all here.)
Post reply on HN