Created
June 30, 2016 08:31
-
-
Save klmr/0dc9e7ba8aa26227e0062e09a39d3529 to your computer and use it in GitHub Desktop.
Using pipes with higher-order functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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
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.
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