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

Side by Side Diff: corelib/src/implementation/collections.dart

Issue 10832060: Add reduce to Collection. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * The [Collections] class implements static methods useful when 6 * The [Collections] class implements static methods useful when
7 * writing a class that implements [Collection] and the [iterator] 7 * writing a class that implements [Collection] and the [iterator]
8 * method. 8 * method.
9 */ 9 */
10 class Collections { 10 class Collections {
Lasse Reichstein Nielsen 2012/08/08 07:19:14 This class should die. Horribly. Instead there sho
Anders Johnsen 2012/11/12 12:02:56 Agreed. I'll gladly create a CL for this in a week
11 static void forEach(Iterable iterable, void f(o)) { 11 static void forEach(Iterable iterable, void f(o)) {
12 for (final e in iterable) { 12 for (final e in iterable) {
13 f(e); 13 f(e);
14 } 14 }
15 } 15 }
16 16
17 static bool some(Iterable iterable, bool f(o)) { 17 static bool some(Iterable iterable, bool f(o)) {
18 for (final e in iterable) { 18 for (final e in iterable) {
19 if (f(e)) return true; 19 if (f(e)) return true;
20 } 20 }
21 return false; 21 return false;
22 } 22 }
23 23
24 static bool every(Iterable iterable, bool f(o)) { 24 static bool every(Iterable iterable, bool f(o)) {
25 for (final e in iterable) { 25 for (final e in iterable) {
26 if (!f(e)) return false; 26 if (!f(e)) return false;
27 } 27 }
28 return true; 28 return true;
29 } 29 }
30 30
31 static List map(Iterable source, List destination, f(o)) { 31 static List map(Iterable source, List destination, f(o)) {
32 for (final e in source) { 32 for (final e in source) {
33 destination.add(f(e)); 33 destination.add(f(e));
34 } 34 }
35 return destination; 35 return destination;
36 } 36 }
37 37
38 static reduce(Iterable iterable, var init, f(prev, element)) {
Lasse Reichstein Nielsen 2012/08/08 07:19:14 Add return value, even if it's just Object. It dif
Anders Johnsen 2012/08/08 07:56:14 Adding var does not provide any further informatio
Lasse Reichstein Nielsen 2012/08/10 11:26:56 Accepted, but not liked. I prefer being explicit a
39 for (final e in iterable) {
Lasse Reichstein Nielsen 2012/08/08 07:19:14 Yes, that means using 'element' instead of 'e'. It
Anders Johnsen 2012/08/08 07:56:14 It's using the style from the rest of the file/lib
Lasse Reichstein Nielsen 2012/08/10 11:26:56 We do. We shoud. Please start here :)
Anders Johnsen 2012/11/12 12:02:56 Done.
40 init = f(init, e);
41 }
42 return init;
43 }
44
38 static List filter(Iterable source, List destination, bool f(o)) { 45 static List filter(Iterable source, List destination, bool f(o)) {
39 for (final e in source) { 46 for (final e in source) {
40 if (f(e)) destination.add(e); 47 if (f(e)) destination.add(e);
41 } 48 }
42 return destination; 49 return destination;
43 } 50 }
44 51
45 static bool isEmpty(Iterable iterable) { 52 static bool isEmpty(Iterable iterable) {
46 return !iterable.iterator().hasNext(); 53 return !iterable.iterator().hasNext();
47 } 54 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 * Returns true if the specified collection contains the specified object 144 * Returns true if the specified collection contains the specified object
138 * reference. 145 * reference.
139 */ 146 */
140 static _containsRef(Collection c, Object ref) { 147 static _containsRef(Collection c, Object ref) {
141 for (var e in c) { 148 for (var e in c) {
142 if (e === ref) return true; 149 if (e === ref) return true;
143 } 150 }
144 return false; 151 return false;
145 } 152 }
146 } 153 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698