30秒学会 Dart 片段 · 2019年7月10日

30秒学会 Dart 片段 – dropWhile

Removes elements from the start of a list until the passed function returns true and returns the remaining elements.

Use List.indexWhere() with the test function to find the first element that returns true.
Return an empty array if there are no matching elements, otherwise use List.sublist() to return the remaining elements.

代码实现

List<T> dropWhile<T>(List<T> lst, bool Function(T) test) {
  int leftIndex = lst.indexWhere(test);
  return leftIndex == -1 ? [] : lst.sublist(leftIndex);
}

使用样例

dropWhile([1, 2, 3, 4], (n) => n >= 3); // [3, 4]