takeRight<T> function
- int n
Returns a function that takes an ImmutableList<T>
and returns
a new ImmutableList<T>
containing only the last n
elements.
The returned function can be used to process lists and take the
last 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 takeLastTwo = takeRight<int>(2);
var newList = takeLastTwo(myList); // Returns ImmutableList([4, 5])
- Parameter
n
: The number of elements to take from the right. - Returns: A function that processes an
ImmutableList<T>
and returns anImmutableList<T>
containing only the lastn
elements.
Implementation
ImmutableList<T> Function(ImmutableList<T>) takeRight<T>(int n) =>
(ImmutableList<T> list) => isOutOfBounds(n, list)
? list
: ImmutableList(list.items.skip(list.items.length - n).toList());