Skip to content

Instantly share code, notes, and snippets.

@zs40x
Created August 18, 2016 04:52
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 zs40x/6bdd56fae588a5dabe7b8142b7896010 to your computer and use it in GitHub Desktop.
Save zs40x/6bdd56fae588a5dabe7b8142b7896010 to your computer and use it in GitHub Desktop.
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