Skip to contents

Constructs an iterator that extracts each unique element in turn from an iterable object. Order of the elements is maintained. This function is an iterator analogue to unique.

Usage

i_unique(object, digest = rlang::hash, ...)

Arguments

object

an iterable object

digest

Optionally specify a custom hash function (e.g. digest::digest, rlang::hash). It should be a function returning a character value.

...

Extra arguments are forwarded to iteror.

Value

an iterator that returns only the unique elements from object

Details

NOTE: In order to determine whether an element is unique, a list of previous unique elements is stored. In doing so, the list can potentially become large if there are a large number of unique elements.

See also

i_dedupe

Examples

it <- i_chain(rep(1, 4), rep(2, 5), 4:7, 2)
as.list(i_unique(it)) # 1 2 4 5 6 7
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 4
#> 
#> [[4]]
#> [1] 5
#> 
#> [[5]]
#> [1] 6
#> 
#> [[6]]
#> [1] 7
#> 

it2 <- iterators::iter(c('a', 'a', "A", "V"))
as.list(i_unique(it2)) # a A V
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> [1] "A"
#> 
#> [[3]]
#> [1] "V"
#> 

x <- as.character(gl(5, 10))
it_unique <- i_unique(x)
as.list(it_unique) # 1 2 3 4 5
#> [[1]]
#> [1] "1"
#> 
#> [[2]]
#> [1] "2"
#> 
#> [[3]]
#> [1] "3"
#> 
#> [[4]]
#> [1] "4"
#> 
#> [[5]]
#> [1] "5"
#>