to method
返回一个 m, n的列表
Example:
3.to(6); // (3, 4, 5, 6)
2.to(-2); // (2, 1, 0, -1, -2)
如果提供了 step ,它将被用作迭代的步长。 step 总是正的,即使迭代的方向在减小。
Example:
8.to(3, by: 2); // (8, 6, 4)
Implementation
List<int> to(int end, {int step = 1}) {
if (step < 1) {
throw ArgumentError(
'Invalid step size: $step. Step size must be greater than 0');
}
final count = ((end + 1 - this).abs() / step).ceil();
// 数值生成函数
final int Function(int) generator = this >= end
? (index) => this - (step * index)
: (index) => this + (step * index);
return List<int>.generate(count, generator);
}