Skip to contents

Constructs an iterator that filters elements from iterable returning only those for which the corresponding element from selectors is TRUE.

Usage

i_mask(object, selectors)

Arguments

object

an iterable object

selectors

an iterable that determines whether the corresponding element in object is returned.

Value

iterator object

Details

The iterator stops when either object or selectors has been exhausted.

Examples

# Filters out odd numbers and retains only even numbers
n <- 10
selectors <- rep(c(FALSE, TRUE), n)
it <- i_mask(seq_len(n), selectors)
as.list(it)
#> [[1]]
#> [1] 2
#> 
#> [[2]]
#> [1] 4
#> 
#> [[3]]
#> [1] 6
#> 
#> [[4]]
#> [1] 8
#> 
#> [[5]]
#> [1] 10
#> 

# Similar idea here but anonymous function is used to filter out even
# numbers
n <- 10
it2 <- i_mask(seq_len(10), rep(c(TRUE, FALSE), n))
as.list(it2)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 3
#> 
#> [[3]]
#> [1] 5
#> 
#> [[4]]
#> [1] 7
#> 
#> [[5]]
#> [1] 9
#> 

it3 <- i_mask(letters, letters %in% c('a', 'e', 'i', 'o', 'u'))
as.list(it3)
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> [1] "e"
#> 
#> [[3]]
#> [1] "i"
#> 
#> [[4]]
#> [1] "o"
#> 
#> [[5]]
#> [1] "u"
#>