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 /** | |
18 * Returns the maximum value in [i], according to the order specified by the | |
19 * [compare] function, or `null` if [i] is empty. | |
20 * | |
21 * The compare function must act as a [Comparator]. If [compare] is omitted, | |
22 * [Comparable.compare] is used. If [i] contains null elements, an exception | |
23 * will be thrown. | |
24 * | |
25 */ | |
26 dynamic max(Iterable i, [Comparator compare = Comparable.compare]) => | |
27 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) > 0 ? a : b); | |
28 | |
29 /** | |
30 * Returns the minimum value in [i], according to the order specified by the | |
31 * [compare] function, or `null` if [i] is empty. | |
32 * | |
33 * The compare function must act as a [Comparator]. If [compare] is omitted, | |
34 * [Comparable.compare] is used. If [i] contains null elements, an exception | |
35 * will be thrown. | |
36 */ | |
37 dynamic min(Iterable i, [Comparator compare = Comparable.compare]) => | |
38 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) < 0 ? a : b); | |
39 | |
40 /** | |
41 * Returns the minimum and maximum values in [i], according to the order | |
42 * specified by the [compare] function, in an [Extent] instance. Always returns | |
43 * an [Extent], but [Extent.min] and [Extent.max] may be `null` if [i] is empty. | |
44 * | |
45 * The compare function must act as a [Comparator]. If [compare] is omitted, | |
46 * [Comparable.compare] is used. If [i] contains null elements, an exception | |
47 * will be thrown. | |
48 * | |
49 * If [i] is empty, an [Extent] is returned with [:null:] values for [:min:] and | |
50 * [:max:], since there are no valid values for them. | |
51 */ | |
52 Extent extent(Iterable i, [Comparator compare = Comparable.compare]) { | |
53 var iterator = i.iterator; | |
54 var hasNext = iterator.moveNext(); | |
55 if (!hasNext) return new Extent(null, null); | |
56 var max = iterator.current; | |
57 var min = iterator.current; | |
58 while (iterator.moveNext()) { | |
59 if (compare(max, iterator.current) < 0) max = iterator.current; | |
60 if (compare(min, iterator.current) > 0) min = iterator.current; | |
61 } | |
62 return new Extent(min, max); | |
63 } | |
64 | |
65 class Extent { | |
66 final min; | |
67 final max; | |
68 Extent(this.min, this.max); | |
69 } | |
OLD | NEW |