Skip to content

Instantly share code, notes, and snippets.

@seaneagan
Created October 27, 2011 18:48
Show Gist options
  • Save seaneagan/1320447 to your computer and use it in GitHub Desktop.
Save seaneagan/1320447 to your computer and use it in GitHub Desktop.
Dart core lib changes
/**
* Proposal: Refactoring Dart collections
*
* Move accessors from Map, List, and Set into ReadableMap, ReadableList, and ReadableSet interfaces which Map, List, and Set extend
*
* "const " prefixed map and list literals produce ReadableMap and ReadableList instances
*
* Let ReadableMap<K, V> extend Collection<K>
*
* Let List extend Queue
*
* Change zero-arg methods to getters where sensible
*
* Supplement Collection interface
*
* Generalize and merge similar methods
*
* Add Range interface with literal syntax support
*
*/
/**
* Collections are Iterables with convenience methods
*/
abstract class CollectionImpl<E> implements Collection<E> {
int get count () {
final int count = 0;
for(final i in this) count++;
return count;
}
bool get empty () => !getIterator().hasNext();
bool contains (E item) {
for(final i in this) {
if(item == i) { // i == item instead ?
return true;
}
return false;
}
}
bool containsIdentical (E item) {
for(final i in this) {
if(item === i) {
return true;
}
return false;
}
}
bool containsAll (Iterable<E> other) {
for(final i in other) {
if(!contains(i)) {
return false;
}
return true;
}
}
void forEach (f(E el));
bool every (bool f(E el));
bool some (bool f(E el));
Collection<E> filter (bool f(E el));
// need generic functions for below methods (see http://code.google.com/p/dart/issues/detail?id=254)
Collection/*<R>*/ map/*<R>*/ (/*<R>*/ f(E el, Collection<E> self));
/*R*/ reduce/*<R>*/ (/*<R>*/ f(/*<R>*/ prev, E curr, Collection<E> self), /*<R>*/ initialValue);
Collection<E> union (Iterable<E> other) => const UnionCollection<E>(this, other);
Collection<E> intersection (Iterable<E> other) => const IntersectionCollection<E>(this, other);
Collection<E> difference (Iterable<E> other) => const DifferenceCollection<E>(this, other);
bool operator == (Iterable<E> other) => this.containsAll(other) && new Collection(other).containsAll(this);
}
/** Collections with no duplicates */
class SetReader<E> extends Collection<E> {
SetReader(Iterable<E> other);
}
/** Writable SetReader */
class Set<E> extends SetReader<E> {
Set([Iterable<E> other]);
void add (E item);
bool remove (E item);
void clear();
}
/** MapReader are [Collection]s of non-duplicate keys mapped to values */
class MapReader<K, V> extends SetReader<K> {
MapReader(MapReader<K, V> other);
Collection<V> get values ( );
V operator [] (K key );
bool operator == (MapReader<E> other ); // same keys, values == for each key
}
/** Maps are writable ReadableMaps */
class Map<K, V> extends MapReader<K, V> {
Map([ReadableMap<K, V> other]);
void operator []= (K key, V value);
V remove(K key);
V putIfAbsent(K key, V value);
void clear();
}
/** ReadableLists are indexed ordered Collections */
class ListReader<E> extends Collection<E> {
ListReader([Iterable<E> other]);
E get first ( );
E get last ( );
ListReader<E> get reverse ( );
int indexOf (E e, [int start] );
int lastIndexOf (E e, [int start] );
ListReader<E> getRange (Range range );
// replaces Strings.join, StringBuffer.toString, Strings.concatAll, see
String join ([String separator]);
// concatenation, replaces String.{concat,+}
ListReader<E> operator + (ListReader<E> other );
V operator [] (K key );
bool operator == (ListReader<E> other ); // same size, values == for each index
}
/** Queues are ListReader which can be written to at the ends */
class Queue<E> extends ListReader<E> {
Queue([Iterable<E> other]);
void addFirst (E e);
void addLast (E e);
E removeFirst ( );
E removeLast ( );
}
/** Lists are Queues which can be written to at any index */
class List<E> extends Queue<E> {
List([Iterable<E> other]);
List.sized([int size]);
set size(int size);
void addAt (E , int at); // allow any index, replaces addLast
void addAllAt (Iterable<E> all, int at, [bool replace/* = false*/]);
Iterable<E> removeRange (Range range );
E removeAt(int index);
void sort (int compare(E, E) );
void operator []= (int index, E value);
void clear();
}
/**
* Dense List of integers
* Literal syntax:
* x..y (x to y)
* x.. (x to infinity)
* ..x (x to -infinity)
* .. (-infinity to infinity or empty set)
*/
class Range extends ReadableList<int> {
/**
* if first is null and last is null, 0 iterations
* last < first is allowed
*/
Range([int first, int last]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment