-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Update fifelse documentation to reflect evaluation behavior #6151
Conversation
From R-CMD-Check: > # fcase can be used for recursion, unlike fifelse
> gcd_dt <- function(x,y) {
+ r <- x
+ return(fcase(!r, y, r, gcd_dt(x, y))) # Recursive call must be in the last when-value pair
+ }
> gcd_dt(10, 1)
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Execution halted not sure why mod operator isn't showing up here, will investigate Edit: Rd files have to use escaped percent symbols, otherwise it marks the beginning of an Rd comment |
@@ -54,5 +59,12 @@ fcase( | |||
x > 5L, 3L, | |||
default = 5L | |||
) | |||
|
|||
# fcase can be used for recursion, unlike fifelse | |||
gcd_dt = function(x,y) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i guess this is greatest common denominator? can you please add a comment to explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me
Closes #4549
fifelse
evaluates bothyes
andno
arguments, regardless of the value oftest
. This makes it unfit for recursive functions as the evaluation will cause an infinite loop of calls.Changed documentation to better reflect this behavior, along with a simple example to showcase the difference against base
ifelse
:Also mentioned that
fcase
can be used instead for recursive calls. Doesfcase
documentation also need updating? I noticed that thewhen
andvalue
pair is evaluated, even if thewhen
doesn't bind, which means:works, but
doesn't, due to the same reason that
fifelse
doesn't.