R/which.R

#' Which stream indices are TRUE?
#'
#' @param stream A stream
#' @param predicate A function that returns TRUE or FALSE
#' @param start_from Find indexes on or after this position
#'
#' @export
stream_which <- function(stream, predicate, start_from = 1L) {
    ind <- start_from

    while(!predicate(stream_car(stream))) {
        ind <- ind + 1L
        stream <- stream_cdr(stream)
    }

    cons_stream(ind,
                stream_which(stream_cdr(stream),
                             predicate,
                             start_from = ind + 1L))
}
tarakc02/lazylist documentation built on May 31, 2019, 3:51 a.m.