fractionalRange method
Returns values in the fractional index range (between 0.0 and 1.0).
Throws ArgumentError if argument is out of range.
Throws StateError if the list is empty.
Examples
import 'package:calc/calc.dart';
void main() {
final list = [1,2,3,4,5];
// Sort values
list.sort();
// Print top 50% of values (4,5).
final top50percent = list.fractionallyGetRange(0.5, 1.0);
print('top 50%: $top50percent');
// Print top 25% of values (5).
final top25percent = list.fractionallyGetRange(0.75, 1.0);
print('top 25%: $top25percent');
}
Implementation
Iterable<T> fractionalRange(double from, double to) {
if (from.isNaN || from < 0.0 || from > 1.0) {
throw ArgumentError.value(from, 'from');
}
if (to.isNaN || to < 0.0 || to > 1.0) {
throw ArgumentError.value(from, 'to');
}
if (to < from) {
throw ArgumentError('to < from');
}
if (isEmpty) {
throw StateError('The iterable is empty');
}
if (to == from) {
return take(0);
}
final fromIndex = (from * (length - 1)).toInt();
final toIndex = (to * length).toInt();
return skip(fromIndex).take(toIndex - fromIndex);
}