Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

give pipe precedence over infix functions #137

Closed
nteetor opened this issue Jan 30, 2017 · 0 comments
Closed

give pipe precedence over infix functions #137

nteetor opened this issue Jan 30, 2017 · 0 comments

Comments

@nteetor
Copy link

nteetor commented Jan 30, 2017

Motivation & Proposal

This request (and the pull request I will subsequently make) is motivated by an issue @mikedecr brought to my attention. Unpacking the result of a pipe sequence with the %<-% operator from the zeallot package did not work as intended.

Here is a small example with a simple infix function, abstracting away zeallot::`%<-%`.

`%<<<%` <- `<-`

a %<<<% 3030 %>% as.character
#> [1] "3030"

a
#> [1] 3030 

The expression prints out "3030" because the result of a %<<<% 3030 is passed to as.character. I propose to give the pipe operator precedence over infix functions.

`%<<<%` <- `<-`

b %<<<% 3030 %>% as.character

b
#> [1] "3030"

Thus, in the second example, %<<<% is passed b and the result of pipe sequence, 3030 %>% as.character. The result is what we would expect if we used <- instead of %<<<%.

Jumping back to zeallot, the new precedence would allow R users to compute values using pipe sequences and then unpack these values. My implementation, however, is generic, handling any infix function.

Implementation

I have implemented this on my fork of magrittr. In my implementation an infix function is treated like a pipe operator until the final assignment block of pipe(). In the assignment block, if an infix function is called, pipe() does not evaluate the LHS, this is left up to the infix function. Similar to how %<>% results in a call to <-, pipe() calls the infix function with the unevaluated LHS and the computed result of the pipe sequence.

Concerns

The helper function %||% is becoming increasingly popular. There may be instances where giving precedence to %>% interferes with usage of %||%.

I hope this issue does not unearth concerns laid to rest in #5.

Tests

I have added tests to cover my additions. Furthermore, all previous tests still pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant