Skip to content

take

Summary

Returns a new list with only the first n elements from xs.

Tip

This function is curried.

Tip

Act as a transducer when composed with drop, filter or map.

Description

Takes the first n elements from xs and returns them in a new list of the same type.

An empty list of the same type is returned when n is either 0, NaN, -Infinity or any other negative number. A full list (of the same type) is returned when n is either Infinity or greater than the number of elements in xs.

Examples

Works with arrays and objects.

1
2
3
4
5
6
7
const take2 = take(2);

take2([1, 2, 3]);
//=> [1, 2]

take2({a: 1, b: 2, c: 3});
//=> {a: 1, b: 2}

Parameters

Name Type Description
n number Number of elements to take.
xs Array or Object List to take from.

Return

Array or Object