Skip to content

Commit

Permalink
Use switch() instead of ifelse() ladders for choices
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam committed Jan 30, 2024
1 parent 6266e6b commit d642146
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#' @param n_offspring A vector of offspring per chain.
#' @return A vector of chain statistics (size/length).
#' @keywords internal
update_chain_stat <- function(stat_type, stat_latest, n_offspring) {
if (stat_type == "size") {
stat_latest <- stat_latest + n_offspring
} else if (stat_type == "length") {
stat_latest <- stat_latest + pmin(1, n_offspring)
}

return(stat_latest)
.update_chain_stat <- function(stat_type, stat_latest, n_offspring) {
return(
switch(
stat_type,
size = stat_latest + n_offspring,
length = stat_latest + pmin(1, n_offspring)
)
)
}

#' Return a function for calculating chain statistics
Expand All @@ -21,13 +21,14 @@ update_chain_stat <- function(stat_type, stat_latest, n_offspring) {
#'
#' @return a function for calculating chain statistics
#' @keywords internal
get_statistic_func <- function(chain_statistic) {
func <- if (chain_statistic == "size") {
rbinom_size
} else if (chain_statistic == "length") {
rgen_length
}
return(func)
.get_statistic_func <- function(chain_statistic) {
return(
switch(
chain_statistic,
size = rbinom_size,
length = rgen_length
)
)
}

#' Construct name of analytical function for estimating loglikelihood of
Expand Down

0 comments on commit d642146

Please sign in to comment.