takeLeft<T> function
- int n
Returns a function that takes an ImmutableList<T>
and returns
a new ImmutableList<T>
containing only the first n
elements.
The returned function can be used to process lists and take the
first n
elements from them.
If n
is out of bounds (less than 0 or greater than the length
of the list), the returned function will return the original list.
Usage:
var myList = ImmutableList([1, 2, 3, 4, 5]);
var takeTwo = takeLeft<int>(2);
var newList = takeTwo(myList); // Returns ImmutableList([1, 2])
- Parameter
n
: The number of elements to take from the left. - Returns: A function that processes an
ImmutableList<T>
and returns anImmutableList<T>
containing only the firstn
elements.
Implementation
ImmutableList<T> Function(ImmutableList<T>) takeLeft<T>(int n) =>
(ImmutableList<T> list) => isOutOfBounds(n, list)
? list
: ImmutableList(list.items.take(n).toList());