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

Side by Side Diff: utils/template/utils.dart

Issue 9695048: Template parser (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix GIT mixup - ugh Created 8 years, 9 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Collection<T> supports most of the ES 5 Array methods, but it's missing
6 // map and reduce.
7
8 // TODO(jmesserly): we might want a version of this that return an iterable,
9 // however JS, Python and Ruby versions are all eager.
10 List map(Iterable source, mapper(source)) {
11 List result = new List();
12 if (source is List) {
13 List list = source; // TODO: shouldn't need this
14 result.length = list.length;
15 for (int i = 0; i < list.length; i++) {
16 result[i] = mapper(list[i]);
17 }
18 } else {
19 for (final item in source) {
20 result.add(mapper(item));
21 }
22 }
23 return result;
24 }
25
26 reduce(Iterable source, callback, [initialValue]) {
27 final i = source.iterator();
28
29 var current = initialValue;
30 if (current == null && i.hasNext()) {
31 current = i.next();
32 }
33 while (i.hasNext()) {
34 current = callback(current, i.next());
35 }
36 return current;
37 }
38
39 List zip(Iterable left, Iterable right, mapper(left, right)) {
40 List result = new List();
41 var x = left.iterator();
42 var y = right.iterator();
43 while (x.hasNext() && y.hasNext()) {
44 result.add(mapper(x.next(), y.next()));
45 }
46 if (x.hasNext() || y.hasNext()) {
47 throw new IllegalArgumentException();
48 }
49 return result;
50 }
51
52 // Color constants used for generating messages.
53 String _GREEN_COLOR = '\u001b[32m';
54 String _RED_COLOR = '\u001b[31m';
55 String _MAGENTA_COLOR = '\u001b[35m';
56 String _NO_COLOR = '\u001b[0m';
57
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698