Skip to content

Instantly share code, notes, and snippets.

@apkd
Created August 30, 2017 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apkd/2810204a45017476d63c146241af171c to your computer and use it in GitHub Desktop.
Save apkd/2810204a45017476d63c146241af171c to your computer and use it in GitHub Desktop.
import { Enumerable } from "powerseq/enumerable";
class KeyValuePair<T> {
key: string;
value: T;
}
class KvpDictionary<T> {
mapping: { [key: string]: T } = {}
public get keys(): Enumerable<string> {
return Enumerable.from(this.getKeys());
}
public get values(): Enumerable<T> {
return Enumerable.from(this.getValues());
}
public get pairs(): Enumerable<KeyValuePair<T>> {
return Enumerable.from(this.getPairs());
}
private *getKeys(): Iterable<string> {
for (let key in this.mapping)
yield key;
}
private *getValues(): Iterable<T> {
for (let key in this.mapping)
yield this.mapping[key];
}
private *getPairs(): Iterable<KeyValuePair<T>> {
for (let key in this.mapping)
yield { key: key, value: this.mapping[key] }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment