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

Side by Side Diff: samples/swarm/swarm_ui_lib/util/CollectionUtils.dart

Issue 11414069: Make mappedBy lazy. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Reupload due to error. Created 8 years, 1 month 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 typedef num NumericValueSelector<T>(T value); 5 typedef num NumericValueSelector<T>(T value);
6 6
7 /** 7 /**
8 * General purpose collection utilities. 8 * General purpose collection utilities.
9 * TODO(jmesserly): make these top level functions? 9 * TODO(jmesserly): make these top level functions?
10 */ 10 */
(...skipping 20 matching lines...) Expand all
31 // TODO(jmesserly): we won't need to do this once List 31 // TODO(jmesserly): we won't need to do this once List
32 // implements insertAt 32 // implements insertAt
33 if (arr is ObservableList) { 33 if (arr is ObservableList) {
34 // TODO(jmesserly): shouldn't need to cast after testing instanceof 34 // TODO(jmesserly): shouldn't need to cast after testing instanceof
35 ObservableList obs = arr; 35 ObservableList obs = arr;
36 obs.recordListInsert(pos, value); 36 obs.recordListInsert(pos, value);
37 } 37 }
38 } 38 }
39 } 39 }
40 40
41 // Collection<T> supports most of the ES 5 list methods, but it's missing
42 // map.
43
44 // TODO(jmesserly): we might want a version of this that return an iterable,
45 // however JS, Python and Ruby versions are all eager.
46 static List mappedBy(Iterable source, var mapper) {
47 // TODO(jmesserly): I was trying to set the capacity here, but instead it
48 // seems to create a fixed list. Hence assigning by index below.
49 List result = new List(source is List ? (source as List).length : null);
50 int i = 0;
51 for (final item in source) {
52 result[i++] = mapper(item);
53 }
54 return result;
55 }
56
57 /** 41 /**
58 * Finds the item in [source] that matches [test]. Returns null if 42 * Finds the item in [source] that matches [test]. Returns null if
59 * no item matches. The typing should be: 43 * no item matches. The typing should be:
60 * T find(Iterable<T> source, bool test(T item)), but we don't have generic 44 * T find(Iterable<T> source, bool test(T item)), but we don't have generic
61 * functions. 45 * functions.
62 */ 46 */
63 static find(Iterable source, bool test(item)) { 47 static find(Iterable source, bool test(item)) {
64 for (final item in source) { 48 for (final item in source) {
65 if (test(item)) return item; 49 if (test(item)) return item;
66 } 50 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 114 }
131 115
132 // TODO(jmesserly): something like should exist on Map, either a method or a 116 // TODO(jmesserly): something like should exist on Map, either a method or a
133 // constructor, see bug #5340679 117 // constructor, see bug #5340679
134 static void copyMap(Map dest, Map source) { 118 static void copyMap(Map dest, Map source) {
135 for (final k in source.keys) { 119 for (final k in source.keys) {
136 dest[k] = source[k]; 120 dest[k] = source[k];
137 } 121 }
138 } 122 }
139 } 123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698