-
Notifications
You must be signed in to change notification settings - Fork 39
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
Syntax to support for expression enclosed by parentheses #38
Comments
The following syntax can be somewhat non-obvious or redundant. x %>>% (fun(.) ~ y)
## can be
x %>>% (f(.)) %>>% (~ y)
x %>>% (m ~ fun(m) ~ y)
# can be
x %>>% (m ~ fun(m)) %>>% (~y) It is better to avoid any side-effect when evaluating enclosed expression within |
sincex %>>% (m) # x[["m"]] why not misunderstand like thisx %>>% (fun(.) ~ y) # x[["y"]] <- fun(x) On Sat, Aug 30, 2014 at 6:21 PM, Kun Ren notifications@github.com wrote:
|
Sorry, the first comment is updated by the second one, and I propose that lambda expressions like x %>>% (fun(.) ~ y) should NOT be supported to perform action that produces side-effect (assignment). I think it's best to restrict all syntax that involves side-effect to start with Clearly Therefore, to be consistent, I suggest the following design principle: ALL expressions in As a result, one who takes a look at the code should be easy to find out mainstream pipeline and distinguish side-effects from it. |
Sorry if I have missed something important. Why use lambda expression rather than a real assignment like this: or, to make side-effect: which I thought should be more explicit as an assignment. |
The For the For Thanks for your suggestion! |
My pleasure! One more thing should be concerned carefully is that when the variable is x %>>% (x = fun(.)) Or more complex: x %>>% (x = fun1(.)) %>>% (x = fun2(.)) Will the latter value overwrite the previous value? Will they affect the |
@yanlinlin82, I've just tried I tried x %>>% (x = fun(.)) and it overwrite |
Now that the syntax x %>>% (z = expr) is equivalent to z <- x %>>% (expr) which means it supports element extracting, lambda expression, etc. |
How about this case:
|
The current implementation at branch Therefore, it is assigning to |
This may look more consistent: x %>>% (~ z) %>>% mean
# z <- x
# mean(x)
x %>>% (~ z = length(.)) %>>% mean
# z <- length(x)
# mean(x)
x %>>% (z = length(.)) %>>% mean
# z <- length(x)
# mean(z)
Does this look ambiguous? mtcars %>>%
(~ lm_mtcars = lm(mpg ~ ., data = .)) %>>%
subset(mpg <= mean(mpg)) %>>%
(~ sub_mtcars) %>>%
(lm_sub_mtcars = lm(mpg ~ ., data = .)) %>>%
summary %>>%
(coefficients) %>>%
(~ coefs_mtcars) |
If |
All right. I guess I could distinguish it now. Let me summarize it as:
In the second case, could |
The rule is currently very definite and can be described as follows: x %>>% (symbol) # where is.symbol(symbol) = TRUE
# getElement(x,"symbol") which supports everything having [[ ]] and S4 objects.
x %>>% (call) # where is.call(call) = TRUE
# see if call is a formula (~) or equals (=) or otherwise
# 1. formula (~)
x %>>% (~ symbol) # symbol <- x; x
x %>>% (~ call) # eval call with . = x as side effect and returns x
x %>>% (symbol ~ call) # eval call with symbol = x
x %>>% (~symbol ~ call) # eval call with symbol = x as side effect and returns x
# 2. equals (=)
x %>>% (y = expr) # y <- x %>>% (expr)
x %>>% (~ y = expr) # y <- x %>>% (expr) as side effect and returns x
# 3. otherwise
x %>>% (call) # pipe to . Therefore, your first point should be if The second point is all right. For the question, here is an example: 1:10 %>>% (~ quote(x)) where |
The text was updated successfully, but these errors were encountered: