OLD | NEW |
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 // Collection<T> supports most of the ES 5 Array methods, but it's missing | 5 // Collection<T> supports most of the ES 5 Array methods, but it's missing |
6 // map and reduce. | 6 // map and reduce. |
7 | 7 |
8 // TODO(jmesserly): we might want a version of this that return an iterable, | 8 // TODO(jmesserly): we might want a version of this that return an iterable, |
9 // however JS, Python and Ruby versions are all eager. | 9 // however JS, Python and Ruby versions are all eager. |
10 List map(Iterable source, mapper(source)) { | 10 List map(Iterable source, mapper(source)) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 * A [FixedCollection] is a collection of [length] items all of which have the | 65 * A [FixedCollection] is a collection of [length] items all of which have the |
66 * identical [value] | 66 * identical [value] |
67 */ | 67 */ |
68 class FixedCollection<E> implements Collection<E> { | 68 class FixedCollection<E> implements Collection<E> { |
69 final E value; | 69 final E value; |
70 final int length; | 70 final int length; |
71 const FixedCollection(this.value, this.length); | 71 const FixedCollection(this.value, this.length); |
72 | 72 |
73 Iterator<E> iterator() => new FixedIterator<E>(value, length); | 73 Iterator<E> iterator() => new FixedIterator<E>(value, length); |
74 void forEach(void f(E element)) { Collections.forEach(this, f); } | 74 void forEach(void f(E element)) { Collections.forEach(this, f); } |
| 75 Collection map(f(E element)) => Collections.map(this, new List(), f); |
75 Collection<E> filter(bool f(E element)) { | 76 Collection<E> filter(bool f(E element)) { |
76 return Collections.filter(this, new List<E>(), f); | 77 return Collections.filter(this, new List<E>(), f); |
77 } | 78 } |
78 bool every(bool f(E element)) => Collections.every(this, f); | 79 bool every(bool f(E element)) => Collections.every(this, f); |
79 bool some(bool f(E element)) => Collections.some(this, f); | 80 bool some(bool f(E element)) => Collections.some(this, f); |
80 bool isEmpty() => length == 0; | 81 bool isEmpty() => length == 0; |
81 } | 82 } |
82 | 83 |
83 class FixedIterator<E> implements Iterator<E> { | 84 class FixedIterator<E> implements Iterator<E> { |
84 final E value; | 85 final E value; |
85 final int length; | 86 final int length; |
86 int _index = 0; | 87 int _index = 0; |
87 FixedIterator(this.value, this.length); | 88 FixedIterator(this.value, this.length); |
88 | 89 |
89 bool hasNext() => _index < length; | 90 bool hasNext() => _index < length; |
90 E next() { | 91 E next() { |
91 _index++; | 92 _index++; |
92 return value; | 93 return value; |
93 } | 94 } |
94 } | 95 } |
95 | 96 |
96 // Color constants used for generating messages. | 97 // Color constants used for generating messages. |
97 String _GREEN_COLOR = '\u001b[32m'; | 98 String _GREEN_COLOR = '\u001b[32m'; |
98 String _RED_COLOR = '\u001b[31m'; | 99 String _RED_COLOR = '\u001b[31m'; |
99 String _MAGENTA_COLOR = '\u001b[35m'; | 100 String _MAGENTA_COLOR = '\u001b[35m'; |
100 String _NO_COLOR = '\u001b[0m'; | 101 String _NO_COLOR = '\u001b[0m'; |
OLD | NEW |