takeRight function

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

Creates a slice of list 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 list, [int n = 1]) {
  if (list.isEmpty) {
    return [];
  }

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