Skip to content

Instantly share code, notes, and snippets.

@eschen42
Created August 8, 2022 21:40
Show Gist options
  • Save eschen42/a541dc1769049f7de872a4387f7ffec5 to your computer and use it in GitHub Desktop.
Save eschen42/a541dc1769049f7de872a4387f7ffec5 to your computer and use it in GitHub Desktop.
Emulate fgrep in R, i.e., is any of needles found in any of haystacks
#' Emulate fgrep, i.e., is any of needles found in any of haystacks
#'
#' This should perform adequately for a few needles.
#'
#' If performance for many needles is inadequate, use
#' `0 < length(AhoCorasickTrie::AhoCorasickSearch(needles, haystacks)[[1]])`
#' as the implementation for a more realistic emulation of fgrep.
#'
#' @param needles A character vector of length 1 or more
#' @param search_strings A character of length 1 or more
#' @return TRUE only when one of needles matches one of search_strings
#' @examples:
#' fgrep(c("sulfate", "phosphate"), c("dimethylsulfate", "salt")) # returns TRUE
#' fgrep(c("sulfate", "phosphate"), c("tangerine", "salt")) # returns FALSE
fgrep <- function(needles, haystacks, ignore_case = FALSE) {
!is.null(
Find(
f = function(s) 0 < sum(
grepl(s, haystacks, fixed = TRUE, ignore.case = ignore_case)
),
x = needles
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment