zatonovo/lambda.r

Shorter lambdas

houshuang opened this issue · 4 comments

I was inspired by this library and by my experience in functional programming in general to try to hack up a shorter lambda syntax for R. %as% didn't work in blocks (with lapply etc). Here's what I came up with, inspired by Haskell and Clojure. It's pretty ugly, but it is a nice proof of concept. Wonder if it would be possible to use defmacro or strmacro to supply the function as a {} block instead of a string... Something for a future lambda.r, if we could make it cleaner/saner?

https://gist.github.com/houshuang/6ff2cb250a91d94ae70f

Hi, thanks for the suggestion, but I'm not quite sure what you're trying to accomplish. If you want an inline lambda expression, why wouldn't you simply use something like function(x) x + 5? I'm a bit bleary-eyed from putting together my discrete math course, so perhaps I'm missing something obvious.

Simply because I wanted something shorter :) I know it's silly, but I am so
used to Haskell, where you would do

folder + 0 [1 2 3] #-> 6

or Clojure where you can do

(map #(% * 2) [1 2 3]) #-> [2 4 6]

or Apple's new Swift where you can do

[1,2,3,4].map {$0 * 2}

After that, lapply(c(1,2,3), function(x) x * 2) seems too verbose :)

On Mon, Jun 9, 2014 at 3:27 PM, Brian Lee Yung Rowe <
notifications@github.com> wrote:

Hi, thanks for the suggestion, but I'm not quite sure what you're trying
to accomplish. If you want an inline lambda expression, why wouldn't you
simply use something like function(x) x + 5? I'm a bit bleary-eyed from
putting together my discrete math course, so perhaps I'm missing something
obvious.


Reply to this email directly or view it on GitHub
#20 (comment).

http://reganmian.net/blog -- Random Stuff that Matters

Updated with some examples

> filter(">2", c(1,2,3,4))
[1] 3 4

> reduce("%+%2", 0, c(1,2,3))
[1] 6

On Mon, Jun 9, 2014 at 3:38 PM, Stian Håklev shaklev@gmail.com wrote:

Simply because I wanted something shorter :) I know it's silly, but I am
so used to Haskell, where you would do

folder + 0 [1 2 3] #-> 6

or Clojure where you can do

(map #(% * 2) [1 2 3]) #-> [2 4 6]

or Apple's new Swift where you can do

[1,2,3,4].map {$0 * 2}

After that, lapply(c(1,2,3), function(x) x * 2) seems too verbose :)

On Mon, Jun 9, 2014 at 3:27 PM, Brian Lee Yung Rowe <
notifications@github.com> wrote:

Hi, thanks for the suggestion, but I'm not quite sure what you're trying
to accomplish. If you want an inline lambda expression, why wouldn't you
simply use something like function(x) x + 5? I'm a bit bleary-eyed from
putting together my discrete math course, so perhaps I'm missing something
obvious.


Reply to this email directly or view it on GitHub
#20 (comment).

http://reganmian.net/blog -- Random Stuff that Matters

http://reganmian.net/blog -- Random Stuff that Matters

Ah okay :) I've thought about how to shorten lambdas as well, but decided it wasn't worth it unless I added some of the other features of lambda.r (e.g. pattern matching) to it. Funny, as I've already been accused of adding "syntactic alum" to R because of lambda.r syntax, so I'm not sure whether this would improve the situation or not. There's probably a way to do it with lazy evaluation and deparsing to make it cleaner, though I'd have to think about what type of interface would make sense.