Skip to main content

Variable: slice()

const slice: {<Start>(start): {<End>(End): <Value>(iterable) => Iterable<Value>; <End, Value>(End, iterable): Iterable<Value>; }; <Start, End>(start, End): <Value>(iterable) => Iterable<Value>; <Start, End, Value>(start, End, iterable): Iterable<Value>; }

Defined in: splices.d.ts:687

Returns an iterable containing the values of iterable between start and end (exclusive) of iterable.

If any part of the range between start and end is outside the bounds of the iterable, then that part is excluded from the returned iterable. Thus, the returned iterable may be empty.

WARNING: This function linearly iterates up to end because iterables do not support random access.

Call Signature​

<Start>(start): {<End>(End): <Value>(iterable) => Iterable<Value>; <End, Value>(End, iterable): Iterable<Value>; }

Type Parameters​

Start​

Start extends number

Parameters​

start​

NonNegativeInteger<Start>

Returns​

<End>(End): <Value>(iterable) => Iterable<Value>

Type Parameters​

End​

End extends number

Parameters​

End​

NonNegativeInteger<End>

Returns​

<Value>(iterable): Iterable<Value>

Type Parameters​
Value​

Value

Parameters​
iterable​

Iterable<Value>

Returns​

Iterable<Value>

<End, Value>(End, iterable): Iterable<Value>

Type Parameters​

End​

End extends number

Value​

Value

Parameters​

End​

NonNegativeInteger<End>

iterable​

Iterable<Value>

Returns​

Iterable<Value>

Call Signature​

<Start, End>(start, End): <Value>(iterable) => Iterable<Value>

Type Parameters​

Start​

Start extends number

End​

End extends number

Parameters​

start​

NonNegativeInteger<Start>

End​

NonNegativeInteger<End>

Returns​

<Value>(iterable): Iterable<Value>

Type Parameters​

Value​

Value

Parameters​

iterable​

Iterable<Value>

Returns​

Iterable<Value>

Call Signature​

<Start, End, Value>(start, End, iterable): Iterable<Value>

Type Parameters​

Start​

Start extends number

End​

End extends number

Value​

Value

Parameters​

start​

NonNegativeInteger<Start>

End​

NonNegativeInteger<End>

iterable​

Iterable<Value>

Returns​

Iterable<Value>

Throws​

if either start or end is not a non-negative integer, or if start is greater than end.

Example​

const iterable = [`sloth`, `more sloth`, `even more sloth`]

console.log(
pipe(
iterable,
slice(0, 3),
reduce(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
pipe(
iterable,
slice(0, 42),
reduce(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
pipe(
iterable,
slice(1, 3),
reduce(toArray()),
),
)
//=> [ 'more sloth', 'even more sloth' ]

console.log(
pipe(
iterable,
slice(3, 5),
reduce(toArray()),
),
)
//=> []

Since​

v3.5.0