takeEveryNth method

  1. @useResult
Iterable<T> takeEveryNth(
  1. int n
)

Takes every n-th element (1-based: first, then 1+n, 1+2n, ...). n must be positive. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
Iterable<T> takeEveryNth(int n) {
  // n < 1 would make the modulo selection meaningless (n == 0 divides by zero,
  // negatives never match), so reject it up front rather than yield garbage.
  if (n < 1) throw ArgumentError(_kErrNPositive, _kParamN);
  // zipWithIndex is 0-based, so position 0 (the first element) satisfies
  // index % n == 0 — that is why "every n-th" keeps the first element and then
  // every n-th after it, matching the 1-based wording in the doc.
  return zipWithIndex().where(((int, T) p) => p.$1 % n == 0).map(((int, T) p) => p.$2);
}