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

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

Issue 13548002: Add Iterable.fold (and Stream.fold) which replace `reduce`. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. 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
« no previous file with comments | « sdk/lib/async/stream.dart ('k') | sdk/lib/core/iterable.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 22 matching lines...) Expand all
33 static bool every(Iterable iterable, bool f(o)) { 33 static bool every(Iterable iterable, bool f(o)) {
34 for (final e in iterable) { 34 for (final e in iterable) {
35 if (!f(e)) return false; 35 if (!f(e)) return false;
36 } 36 }
37 return true; 37 return true;
38 } 38 }
39 39
40 static dynamic reduce(Iterable iterable, 40 static dynamic reduce(Iterable iterable,
41 dynamic initialValue, 41 dynamic initialValue,
42 dynamic combine(dynamic previousValue, element)) { 42 dynamic combine(dynamic previousValue, element)) {
43 return fold(iterable, initialValue, combine);
44 }
45
46 static dynamic fold(Iterable iterable,
47 dynamic initialValue,
48 dynamic combine(dynamic previousValue, element)) {
43 for (final element in iterable) { 49 for (final element in iterable) {
44 initialValue = combine(initialValue, element); 50 initialValue = combine(initialValue, element);
45 } 51 }
46 return initialValue; 52 return initialValue;
47 } 53 }
48 54
49 /** 55 /**
50 * Simple implementation for [Collection.removeAll]. 56 * Simple implementation for [Collection.removeAll].
51 * 57 *
52 * This implementation assumes that [Collection.remove] on [collection] 58 * This implementation assumes that [Collection.remove] on [collection]
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 * The source of the elements may be a [List] or any [Iterable] with 428 * The source of the elements may be a [List] or any [Iterable] with
423 * efficient [Iterable.length] and [Iterable.elementAt]. 429 * efficient [Iterable.length] and [Iterable.elementAt].
424 */ 430 */
425 class UnmodifiableListView<E> extends UnmodifiableListBase<E> { 431 class UnmodifiableListView<E> extends UnmodifiableListBase<E> {
426 Iterable<E> _source; 432 Iterable<E> _source;
427 /** Create an unmodifiable list backed by [source]. */ 433 /** Create an unmodifiable list backed by [source]. */
428 UnmodifiableListView(Iterable<E> source) : _source = source; 434 UnmodifiableListView(Iterable<E> source) : _source = source;
429 int get length => _source.length; 435 int get length => _source.length;
430 E operator[](int index) => _source.elementAt(index); 436 E operator[](int index) => _source.elementAt(index);
431 } 437 }
OLDNEW
« no previous file with comments | « sdk/lib/async/stream.dart ('k') | sdk/lib/core/iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698