Skip to contents

Returns the first n elements of an iterable object as a list. If n is larger than the number of elements in object, the entire iterator is consumed.

Usage

take(obj, n = 1, mode = "list", ...)

# S3 method for default
take(obj, n = 1, mode = "list", ...)

# S3 method for iteror
take(obj, n = 1, mode = "list", ...)

Arguments

obj

An iterable object.

n

The maximum number of elements to extract from the iteror.

mode

The mode of vector to return.

...

Further arguments may be passed along to the iteror constructor.

Value

a list of the first n items of the iterable obj

Details

A function take first appeared in package itertools2. It is basically an alias for as.list but defaults to n=1.

See also

concat as.vector.iteror

as.vector.iteror

Examples

take(1:10, 3) # 1 2 3
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
take(icount(), 10) # 1:10
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 4
#> 
#> [[5]]
#> [1] 5
#> 
#> [[6]]
#> [1] 6
#> 
#> [[7]]
#> [1] 7
#> 
#> [[8]]
#> [1] 8
#> 
#> [[9]]
#> [1] 9
#> 
#> [[10]]
#> [1] 10
#> 
take(icount(5), 10) # 1 2 3 4 5
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 4
#> 
#> [[5]]
#> [1] 5
#>