OLD | NEW |
| (Empty) |
1 // Copyright 2013 Google Inc. All Rights Reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 part of quiver.iterables; | |
16 | |
17 /// Returns the maximum value in [i], according to the order specified by the | |
18 /// [compare] function, or `null` if [i] is empty. | |
19 /// | |
20 /// The compare function must act as a [Comparator]. If [compare] is omitted, | |
21 /// [Comparable.compare] is used. If [i] contains null elements, an exception | |
22 /// will be thrown. | |
23 /// | |
24 dynamic max(Iterable i, [Comparator compare = Comparable.compare]) => | |
25 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) > 0 ? a : b); | |
26 | |
27 /// Returns the minimum value in [i], according to the order specified by the | |
28 /// [compare] function, or `null` if [i] is empty. | |
29 /// | |
30 /// The compare function must act as a [Comparator]. If [compare] is omitted, | |
31 /// [Comparable.compare] is used. If [i] contains null elements, an exception | |
32 /// will be thrown. | |
33 dynamic min(Iterable i, [Comparator compare = Comparable.compare]) => | |
34 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) < 0 ? a : b); | |
35 | |
36 /// Returns the minimum and maximum values in [i], according to the order | |
37 /// specified by the [compare] function, in an [Extent] instance. Always returns | |
38 /// an [Extent], but [Extent.min] and [Extent.max] may be `null` if [i] is empty
. | |
39 /// | |
40 /// The compare function must act as a [Comparator]. If [compare] is omitted, | |
41 /// [Comparable.compare] is used. If [i] contains null elements, an exception | |
42 /// will be thrown. | |
43 /// | |
44 /// If [i] is empty, an [Extent] is returned with [:null:] values for [:min:] an
d | |
45 /// [:max:], since there are no valid values for them. | |
46 Extent extent(Iterable i, [Comparator compare = Comparable.compare]) { | |
47 var iterator = i.iterator; | |
48 var hasNext = iterator.moveNext(); | |
49 if (!hasNext) return new Extent(null, null); | |
50 var max = iterator.current; | |
51 var min = iterator.current; | |
52 while (iterator.moveNext()) { | |
53 if (compare(max, iterator.current) < 0) max = iterator.current; | |
54 if (compare(min, iterator.current) > 0) min = iterator.current; | |
55 } | |
56 return new Extent(min, max); | |
57 } | |
58 | |
59 class Extent { | |
60 final min; | |
61 final max; | |
62 Extent(this.min, this.max); | |
63 } | |
OLD | NEW |