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