Last active
November 25, 2015 17:10
-
-
Save kastermester/ee2d1e22608d53f6c405 to your computer and use it in GitHub Desktop.
Dummy immutable list implementation in typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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