dropRight<T> function
Creates a slice of list with n elements dropped from the end.
- @param {List} list The list to query.
- @param {int}
n=1The number of elements to drop. - @returns {List} Returns the slice of
list.
@example
dropRight([1, 2, 3]);
// => [1, 2]
dropRight([1, 2, 3], 2);
// => [1]
dropRight([1, 2, 3], 5);
// => []
dropRight([1, 2, 3], 0);
// => [1, 2, 3]
Implementation
List<T> dropRight<T>(List<T> list, [int n = 1]) {
if (n < 1 || list.isEmpty) {
return list.isEmpty ? [] : List.from(list);
}
if (n >= list.length) {
return [];
}
return list.sublist(0, list.length - n);
}