Files
sdk/utils/template/utils.dart
T
terry@google.com 91dd628bd8 More changes for CSS to run under VM.
Need unique name for library collision with template parser.

Few more changes to work under VM.

More changes for lang.dart seperation.

Seperation from lang.dart (tokenizer can't access private fields of base class in another library).  This was allowed in frog but not in the VM.  VM is right so need to sever dependency.

BUG=
TEST=

Review URL: https://chromiumcodereview.appspot.com//9695048

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@5544 260f80e4-7a28-3924-810f-c04153c831b5
2012-03-15 21:19:54 +00:00

53 lines
1.4 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Collection<T> supports most of the ES 5 Array methods, but it's missing
// map and reduce.
// TODO(jmesserly): we might want a version of this that return an iterable,
// however JS, Python and Ruby versions are all eager.
List map(Iterable source, mapper(source)) {
List result = new List();
if (source is List) {
List list = source; // TODO: shouldn't need this
result.length = list.length;
for (int i = 0; i < list.length; i++) {
result[i] = mapper(list[i]);
}
} else {
for (final item in source) {
result.add(mapper(item));
}
}
return result;
}
reduce(Iterable source, callback, [initialValue]) {
final i = source.iterator();
var current = initialValue;
if (current == null && i.hasNext()) {
current = i.next();
}
while (i.hasNext()) {
current = callback(current, i.next());
}
return current;
}
List zip(Iterable left, Iterable right, mapper(left, right)) {
List result = new List();
var x = left.iterator();
var y = right.iterator();
while (x.hasNext() && y.hasNext()) {
result.add(mapper(x.next(), y.next()));
}
if (x.hasNext() || y.hasNext()) {
throw new IllegalArgumentException();
}
return result;
}