takeRight function

List takeRight (
  1. List array,
  2. [int n = 1]
)

Creates a slice of array with n elements taken from the end. Example

takeRight([1, 2, 3])
// => [3]

takeRight([1, 2, 3], 2)
// => [2, 3]

takeRight([1, 2, 3], 5)
// => [1, 2, 3]

takeRight([1, 2, 3], 0)
// => []

Implementation

List takeRight(List array, [int n = 1]) {
  if (array.isEmpty) {
    return [];
  }

  n = array.length - n;
  return slice(array, n < 0 ? 0 : n, array.length);
}