Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class List<T> {
private let listItems: [T]
init() {
self.listItems = [T]()
}
init(listItems: [T]) {
self.listItems = listItems
}
static func with(list: List<T>, item: T) -> List<T> {
return list.add(item)
}
func add(item: T) -> List<T> {
var listWithAddedItem = self.listItems
listWithAddedItem.append(item)
return List<T>(listItems: listWithAddedItem)
}
func count() -> Int {
return self.listItems.count
}
func items() -> [T] {
return self.listItems
}
}
let staticExampleFirstList =
List<Int>.with(List<Int>(), item: 1)
staticExampleFirstList.items() // [1]
let anIntList = List<Int>(listItems: [0,9])
anIntList.items() // [0, 9]
let staticExampleAnotherList =
List<Int>.with(anIntList, item: 1).add(5).add(10)
staticExampleAnotherList.items() // [0, 9, 1, 5, 10]
staticExampleFirstList.items() // [1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment