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

Update fifelse documentation to reflect evaluation behavior #6151

Merged
merged 11 commits into from
Jun 3, 2024
Merged

Conversation

joshhwuu
Copy link
Member

@joshhwuu joshhwuu commented May 27, 2024

Closes #4549

fifelse evaluates both yes and no arguments, regardless of the value of test. 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:

ifelse(1 == 1, print("yes"), print("no"))
# [1] "yes"
fifelse(1 == 1, print("yes"), print("no"))
# [1] "yes"
# [1] "no"

Also mentioned that fcase can be used instead for recursive calls. Does fcase documentation also need updating? I noticed that the when and value pair is evaluated, even if the when doesn't bind, which means:

gcd_dt <- function(x,y) {
  r <- x%%y;
  return(data.table::fcase(!r, y, r, gcd_dt(x, y)))
}

works, but

gcd_dt <- function(x,y) {
  r <- x%%y;
  return(data.table::fcase(!r, y, r, gcd_dt(x, y))) # (when value pair swapped)
}

doesn't, due to the same reason that fifelse doesn't.

@joshhwuu joshhwuu requested a review from Anirban166 May 30, 2024 20:47
@joshhwuu
Copy link
Member Author

joshhwuu commented Jun 1, 2024

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) {
Copy link
Member

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?

Copy link
Member

@tdhock tdhock left a 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

@tdhock tdhock merged commit 8d1377e into master Jun 3, 2024
3 checks passed
@joshhwuu joshhwuu deleted the fifdoc branch June 3, 2024 21:02
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

Successfully merging this pull request may close these issues.

fifelse did not work in recursive function
3 participants