Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: packages/quiver_iterables/lib/src/min_max.dart

Issue 2119523002: Added full js & js_util packages (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 Google Inc. All Rights Reserved. 1 // Copyright 2013 Google Inc. All Rights Reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 part of quiver.iterables; 15 part of quiver.iterables;
16 16
17 /** 17 /// Returns the maximum value in [i], according to the order specified by the
18 * Returns the maximum value in [i], according to the order specified by the 18 /// [compare] function, or `null` if [i] is empty.
19 * [compare] function, or `null` if [i] is empty. 19 ///
20 * 20 /// The compare function must act as a [Comparator]. If [compare] is omitted,
21 * 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 * [Comparable.compare] is used. If [i] contains null elements, an exception 22 /// will be thrown.
23 * will be thrown. 23 ///
24 *
25 */
26 dynamic max(Iterable i, [Comparator compare = Comparable.compare]) => 24 dynamic max(Iterable i, [Comparator compare = Comparable.compare]) =>
27 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) > 0 ? a : b); 25 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) > 0 ? a : b);
28 26
29 /** 27 /// Returns the minimum value in [i], according to the order specified by the
30 * Returns the minimum value in [i], according to the order specified by the 28 /// [compare] function, or `null` if [i] is empty.
31 * [compare] function, or `null` if [i] is empty. 29 ///
32 * 30 /// The compare function must act as a [Comparator]. If [compare] is omitted,
33 * The compare function must act as a [Comparator]. If [compare] is omitted, 31 /// [Comparable.compare] is used. If [i] contains null elements, an exception
34 * [Comparable.compare] is used. If [i] contains null elements, an exception 32 /// will be thrown.
35 * will be thrown.
36 */
37 dynamic min(Iterable i, [Comparator compare = Comparable.compare]) => 33 dynamic min(Iterable i, [Comparator compare = Comparable.compare]) =>
38 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) < 0 ? a : b); 34 i.isEmpty ? null : i.reduce((a, b) => compare(a, b) < 0 ? a : b);
39 35
40 /** 36 /// Returns the minimum and maximum values in [i], according to the order
41 * Returns the minimum and maximum values in [i], according to the order 37 /// specified by the [compare] function, in an [Extent] instance. Always returns
42 * 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 .
43 * an [Extent], but [Extent.min] and [Extent.max] may be `null` if [i] is empty. 39 ///
44 * 40 /// The compare function must act as a [Comparator]. If [compare] is omitted,
45 * The compare function must act as a [Comparator]. If [compare] is omitted, 41 /// [Comparable.compare] is used. If [i] contains null elements, an exception
46 * [Comparable.compare] is used. If [i] contains null elements, an exception 42 /// will be thrown.
47 * will be thrown. 43 ///
48 * 44 /// If [i] is empty, an [Extent] is returned with [:null:] values for [:min:] an d
49 * If [i] is empty, an [Extent] is returned with [:null:] values for [:min:] and 45 /// [:max:], since there are no valid values for them.
50 * [:max:], since there are no valid values for them.
51 */
52 Extent extent(Iterable i, [Comparator compare = Comparable.compare]) { 46 Extent extent(Iterable i, [Comparator compare = Comparable.compare]) {
53 var iterator = i.iterator; 47 var iterator = i.iterator;
54 var hasNext = iterator.moveNext(); 48 var hasNext = iterator.moveNext();
55 if (!hasNext) return new Extent(null, null); 49 if (!hasNext) return new Extent(null, null);
56 var max = iterator.current; 50 var max = iterator.current;
57 var min = iterator.current; 51 var min = iterator.current;
58 while (iterator.moveNext()) { 52 while (iterator.moveNext()) {
59 if (compare(max, iterator.current) < 0) max = iterator.current; 53 if (compare(max, iterator.current) < 0) max = iterator.current;
60 if (compare(min, iterator.current) > 0) min = iterator.current; 54 if (compare(min, iterator.current) > 0) min = iterator.current;
61 } 55 }
62 return new Extent(min, max); 56 return new Extent(min, max);
63 } 57 }
64 58
65 class Extent { 59 class Extent {
66 final min; 60 final min;
67 final max; 61 final max;
68 Extent(this.min, this.max); 62 Extent(this.min, this.max);
69 } 63 }
OLDNEW
« no previous file with comments | « packages/quiver_iterables/lib/src/merge.dart ('k') | packages/quiver_iterables/lib/src/partition.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698