Skip to content

Commit

Permalink
Add a comment about NaN in min/max extensions (#234)
Browse files Browse the repository at this point in the history
* Add a comment about the treatment of NaN in `min`/`max` extensions on number types.
  • Loading branch information
slovnicki committed Apr 8, 2022
1 parent 2bbb27b commit c1a07e4
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/src/iterable_extensions.dart
Expand Up @@ -592,6 +592,8 @@ extension IterableNullableExtension<T extends Object> on Iterable<T?> {
/// since doubles require special handling of [double.nan].
extension IterableNumberExtension on Iterable<num> {
/// A minimal element of the iterable, or `null` it the iterable is empty.
///
/// If any element is [NaN](double.nan), the result is NaN.
num? get minOrNull {
var iterator = this.iterator;
if (iterator.moveNext()) {
Expand All @@ -615,10 +617,14 @@ extension IterableNumberExtension on Iterable<num> {

/// A minimal element of the iterable.
///
/// If any element is [NaN](double.nan), the result is NaN.
///
/// The iterable must not be empty.
num get min => minOrNull ?? (throw StateError('No element'));

/// A maximal element of the iterable, or `null` if the iterable is empty.
///
/// If any element is [NaN](double.nan), the result is NaN.
num? get maxOrNull {
var iterator = this.iterator;
if (iterator.moveNext()) {
Expand All @@ -642,6 +648,8 @@ extension IterableNumberExtension on Iterable<num> {

/// A maximal element of the iterable.
///
/// If any element is [NaN](double.nan), the result is NaN.
///
/// The iterable must not be empty.
num get max => maxOrNull ?? (throw StateError('No element'));

Expand Down Expand Up @@ -765,6 +773,8 @@ extension IterableIntegerExtension on Iterable<int> {
/// [IterableComparableExtension] since doubles are only `Comparable<num>`.
extension IterableDoubleExtension on Iterable<double> {
/// A minimal element of the iterable, or `null` it the iterable is empty.
///
/// If any element is [NaN](double.nan), the result is NaN.
double? get minOrNull {
var iterator = this.iterator;
if (iterator.moveNext()) {
Expand All @@ -788,10 +798,14 @@ extension IterableDoubleExtension on Iterable<double> {

/// A minimal element of the iterable.
///
/// If any element is [NaN](double.nan), the result is NaN.
///
/// The iterable must not be empty.
double get min => minOrNull ?? (throw StateError('No element'));

/// A maximal element of the iterable, or `null` if the iterable is empty.
///
/// If any element is [NaN](double.nan), the result is NaN.
double? get maxOrNull {
var iterator = this.iterator;
if (iterator.moveNext()) {
Expand All @@ -815,6 +829,8 @@ extension IterableDoubleExtension on Iterable<double> {

/// A maximal element of the iterable.
///
/// If any element is [NaN](double.nan), the result is NaN.
///
/// The iterable must not be empty.
double get max => maxOrNull ?? (throw StateError('No element'));

Expand Down

0 comments on commit c1a07e4

Please sign in to comment.