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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 * A [FixedCollection] is a collection of [length] items all of which have the | 115 * A [FixedCollection] is a collection of [length] items all of which have the |
116 * identical [value] | 116 * identical [value] |
117 */ | 117 */ |
118 class FixedCollection<E> implements Collection<E> { | 118 class FixedCollection<E> implements Collection<E> { |
119 final E value; | 119 final E value; |
120 final int length; | 120 final int length; |
121 const FixedCollection(this.value, this.length); | 121 const FixedCollection(this.value, this.length); |
122 | 122 |
123 Iterator<E> iterator() => new FixedIterator<E>(value, length); | 123 Iterator<E> iterator() => new FixedIterator<E>(value, length); |
124 void forEach(void f(E element)) { Collections.forEach(this, f); } | 124 void forEach(void f(E element)) { Collections.forEach(this, f); } |
125 Collection map(f(E element)) { | |
sra1
2012/01/05 22:46:49
nit: prefer =>, especially if it fits on one line.
| |
126 return Collections.map(this, new List(), f); | |
127 } | |
125 Collection<E> filter(bool f(E element)) { | 128 Collection<E> filter(bool f(E element)) { |
126 return Collections.filter(this, new List<E>(), f); | 129 return Collections.filter(this, new List<E>(), f); |
127 } | 130 } |
128 bool every(bool f(E element)) => Collections.every(this, f); | 131 bool every(bool f(E element)) => Collections.every(this, f); |
129 bool some(bool f(E element)) => Collections.some(this, f); | 132 bool some(bool f(E element)) => Collections.some(this, f); |
130 bool isEmpty() => length == 0; | 133 bool isEmpty() => length == 0; |
131 } | 134 } |
132 | 135 |
133 class FixedIterator<E> implements Iterator<E> { | 136 class FixedIterator<E> implements Iterator<E> { |
134 final E value; | 137 final E value; |
135 final int length; | 138 final int length; |
136 int _index = 0; | 139 int _index = 0; |
137 FixedIterator(this.value, this.length); | 140 FixedIterator(this.value, this.length); |
138 | 141 |
139 bool hasNext() => _index < length; | 142 bool hasNext() => _index < length; |
140 E next() { | 143 E next() { |
141 _index++; | 144 _index++; |
142 return value; | 145 return value; |
143 } | 146 } |
144 } | 147 } |
145 | 148 |
146 // Color constants used for generating messages. | 149 // Color constants used for generating messages. |
147 String _GREEN_COLOR = '\u001b[32m'; | 150 String _GREEN_COLOR = '\u001b[32m'; |
148 String _RED_COLOR = '\u001b[31m'; | 151 String _RED_COLOR = '\u001b[31m'; |
149 String _MAGENTA_COLOR = '\u001b[35m'; | 152 String _MAGENTA_COLOR = '\u001b[35m'; |
150 String _NO_COLOR = '\u001b[0m'; | 153 String _NO_COLOR = '\u001b[0m'; |
OLD | NEW |