Skip to content

Instantly share code, notes, and snippets.

@klmr
Created June 30, 2016 08:31
Show Gist options
  • Save klmr/0dc9e7ba8aa26227e0062e09a39d3529 to your computer and use it in GitHub Desktop.
Save klmr/0dc9e7ba8aa26227e0062e09a39d3529 to your computer and use it in GitHub Desktop.
Using pipes with higher-order functions
# Higher order function that returns a function:
make_sum = function () {
# Some code here …
message('Requesting to calculate a sum')
sum
}
seq(1, 10) %>% sum
# Now, logically, what does this do?
seq(1, 10) %>% make_sum()
# Correct:
seq(1, 10) %>% make_sum()()
@smbache
Copy link

smbache commented Jun 30, 2016

seq(1, 10) %>% (make_sum())

@smbache
Copy link

smbache commented Jun 30, 2016

the pipe will evaluate rhs first, if it is parenthesized.. Can also be used if RHS e.g. generates a call.

@klmr
Copy link
Author

klmr commented Jun 30, 2016

Sure, I’m not saying that there’s no solution; all I’m saying that not using parentheses is inconsistent, because if foo %>% bar executes bar(foo), then I expect foo %>% bar() to execute bar()(foo). It’s a stumbling block in the API, nothing more, nothing less.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment