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

Consider a consistent methodology for applying different map functions #133

Closed
uriahf opened this issue Jun 23, 2024 · 1 comment
Closed

Comments

@uriahf
Copy link
Owner

uriahf commented Jun 23, 2024

So far I used a lot of if-else conditions

https://github.com/uriahf/rtichoke/blob/09428c277cc0837cfe486ae20ee0375f2b1c6d2e/R/prepare_performance_data.R#L115C1-L116C1

@uriahf
Copy link
Owner Author

uriahf commented Jun 26, 2024

Seems like proper solution

library(purrr)

addition <- function (a_vec, b_vec) {
  a_vec + b_vec
}

multi <- function (a_vec, b_vec) {
  a_vec * b_vec
}

run_function_over_a_b_lists <- function(a, b, func) {
  
  if (length(b) > 1) {
    
    map2(a, b, func)
    
  } else {
    
    map(a, \(x) func(x, b_vec=b[[1]]))
  }
  
  
}

run_function_over_a_b_lists( 
  list("train" = c(1, 2, 3),
       "test" = c(1, 2, 3) +1), 
  list("train" = c(1, 2, 3),
       "test" = c(1, 2, 3) + 2), addition )

run_function_over_a_b_lists( 
  list("train" = c(1, 2, 3),
       "test" = c(1, 2, 3) +1), 
  list("train" = c(1, 2, 0)), addition )

run_function_over_a_b_lists( 
  list("train" = c(1, 2, 3),
       "test" = c(1, 2, 3) +1), 
  list("train" = c(1, 2, 3),
       "test" = c(1, 2, 3) + 2), multi )


run_function_over_a_b_lists( 
  list("train" = c(1, 2, 3),
       "test" = c(1, 2, 3) +1), 
  list("train" = c(1, 2, 0)), multi )

@uriahf uriahf closed this as completed Jun 26, 2024
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