Skip to content

Instantly share code, notes, and snippets.

@kastermester
Last active November 25, 2015 17:10
Show Gist options
  • Save kastermester/ee2d1e22608d53f6c405 to your computer and use it in GitHub Desktop.
Save kastermester/ee2d1e22608d53f6c405 to your computer and use it in GitHub Desktop.
Dummy immutable list implementation in typescript
class List<T> {
_list: T[];
constructor(list: T[]){
this._list = list;
}
push(value: T) : List<T> {
var newValues = this._list.slice(0);
newValues.push(value);
return new List<T>(newValues);
}
pop() : List<T> {
var newValues = this._list.slice(0);
newValues.pop();
return new List<T>(newValues);
}
length() : number {
return this._list.length;
}
get(index: number) : T {
return this._list[index];
}
}
type someObject = {
x: number,
y: number
};
var l = new List<someObject>([]);
var newList = l.push({
x: 5,
y: 10
});
console.log(newList.length());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment