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

Side by Side Diff: sdk/lib/collection/collections.dart

Issue 14071002: Added new version of reduce. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removed more uses of max, and a few bugs. Created 7 years, 8 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 part of dart.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * This class provides default implementations for Iterables (including Lists). 8 * This class provides default implementations for Iterables (including Lists).
9 * 9 *
10 * Once Dart receives Mixins it will be replaced with mixin classes. 10 * Once Dart receives Mixins it will be replaced with mixin classes.
(...skipping 21 matching lines...) Expand all
32 } 32 }
33 33
34 static bool every(Iterable iterable, bool f(o)) { 34 static bool every(Iterable iterable, bool f(o)) {
35 for (final e in iterable) { 35 for (final e in iterable) {
36 if (!f(e)) return false; 36 if (!f(e)) return false;
37 } 37 }
38 return true; 38 return true;
39 } 39 }
40 40
41 static dynamic reduce(Iterable iterable, 41 static dynamic reduce(Iterable iterable,
42 dynamic initialValue, 42 dynamic combine(previousValue, element)) {
43 dynamic combine(dynamic previousValue, element)) { 43 Iterator iterator = iterable.iterator;
44 return fold(iterable, initialValue, combine); 44 if (!iterator.moveNext()) throw new StateError("No elements");
45 var value = iterator.current;
46 while (iterator.moveNext()) {
47 value = combine(value, iterator.current);
48 }
49 return value;
45 } 50 }
46 51
47 static dynamic fold(Iterable iterable, 52 static dynamic fold(Iterable iterable,
48 dynamic initialValue, 53 dynamic initialValue,
49 dynamic combine(dynamic previousValue, element)) { 54 dynamic combine(dynamic previousValue, element)) {
50 for (final element in iterable) { 55 for (final element in iterable) {
51 initialValue = combine(initialValue, element); 56 initialValue = combine(initialValue, element);
52 } 57 }
53 return initialValue; 58 return initialValue;
54 } 59 }
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 * The source of the elements may be a [List] or any [Iterable] with 434 * The source of the elements may be a [List] or any [Iterable] with
430 * efficient [Iterable.length] and [Iterable.elementAt]. 435 * efficient [Iterable.length] and [Iterable.elementAt].
431 */ 436 */
432 class UnmodifiableListView<E> extends UnmodifiableListBase<E> { 437 class UnmodifiableListView<E> extends UnmodifiableListBase<E> {
433 Iterable<E> _source; 438 Iterable<E> _source;
434 /** Create an unmodifiable list backed by [source]. */ 439 /** Create an unmodifiable list backed by [source]. */
435 UnmodifiableListView(Iterable<E> source) : _source = source; 440 UnmodifiableListView(Iterable<E> source) : _source = source;
436 int get length => _source.length; 441 int get length => _source.length;
437 E operator[](int index) => _source.elementAt(index); 442 E operator[](int index) => _source.elementAt(index);
438 } 443 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698