Skip to content

Commit

Permalink
Merge pull request #13 from Smit-tay/RS_foo_finder
Browse files Browse the repository at this point in the history
Fix find_previous_vector_element
  • Loading branch information
RobertASmith authored Jan 30, 2024
2 parents 5963845 + 628e783 commit d31204d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
47 changes: 27 additions & 20 deletions R/cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
#' find_next_vector_element(value = 5, vector = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
#' }
#'
find_next_vector_element <- function(value, vector){
greater_than <- vector[vector > value]
find_next_vector_element <- function(value, vector) {
# Find the elements in the vector that are greater than the specified value
greater_than_value <- vector[vector > value]

if(length(greater_than) == 0){
return(NA)
# If there are no elements greater than the specified value, return NULL or a default value
if (length(greater_than_value) == 0) {
return(max(vector)) # or return a default value as needed
}

next_one <- min(greater_than, na.rm = T)
# Find the minimum value among the elements greater than the specified value
next_element <- min(greater_than_value)

return(next_one)
return(next_element)
}


#' @title Find the previous element of the vector before a value
#' @description Find the previous element of the vector before a value
#' @param value A value of numeric values
Expand All @@ -33,11 +37,19 @@ find_next_vector_element <- function(value, vector){
#' }
#'
find_previous_vector_element <- function(value, vector){
greater_than <- vector[vector < value]
previous_one <- max(greater_than)
return(previous_one)
}
# Find the elements in the vector that are less than the specified value
less_than_value <- vector[vector < value]

# If there are no elements less than the specified value, return the value
if (length(less_than_value) == 0) {
return(value)
}

# Find the maximum value among the elements less than the specified value
previous_element <- max(less_than_value)

return(previous_element)
}



Expand Down Expand Up @@ -118,16 +130,11 @@ extract_function_name2 <- function(string){
#assign_operand_locations <- stringr::str_locate_all(pattern = c("=|<-"),
# string = string)[[1]][, "start"]

#foo_assign_operand_location <- find_previous_vector_element(value = foo_def_start,
# vector = assign_operand_locations)

v_chars <- substr(x = string,
start = 1,
stop = foo_assign_operand_location - 1) |>
stringr::str_replace_all(pattern = c("\n"),
replacement = " ") |>
strsplit(split = " ") |>
unlist()
foo_name <- substr(string, 1, foo_assign_operand_location-1)
foo_name <- stringr::str_replace_all(string = foo_name, pattern = c("\n"), replacement = " ")
foo_name <- strsplit(x = foo_name, split = " ")
foo_name <- unlist(x = foo_name)
foo_name <- utils::tail(x = foo_name, n = 1)


foo_name <- v_chars[which(!(v_chars %in% c("", "=", "<-")))] |>
Expand Down
20 changes: 9 additions & 11 deletions tests/testthat/test-cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,19 @@ test_that("Extracting function names works as intended",


test_that("Next element after integer in vector works as intended",
{
expect_equal(find_next_vector_element(10, 1:12), 11)
expect_equal(find_next_vector_element(4, seq(1, 120, 5)), 6)
{
expect_equal(find_next_vector_element(10, 1:12), 11)
expect_equal(find_next_vector_element(4, seq(1, 120, 5)), 6)
expect_equal(find_next_vector_element(120, seq(1, 120, 5)), 116)
})






test_that("Previous element before integer in vector works as intended",
{
expect_equal(find_previous_vector_element(10, 1:12), 9)
expect_equal(find_previous_vector_element(4, seq(1, 120, 5)), 1)
})
{
expect_equal(find_previous_vector_element(10, 1:12), 9)
expect_equal(find_previous_vector_element(4, seq(1, 120, 5)), 1)
expect_equal(find_previous_vector_element(1, seq(1, 120, 5)), 1)
})



Expand Down

0 comments on commit d31204d

Please sign in to comment.