Skip to content

Commit 7a5700d

Browse files
committed
added ExpressibleByArrayLiteral to the Linked List and wrote test for the new initializer.
1 parent cae6d08 commit 7a5700d

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class LinkedListNode<T> {
1010
}
1111
}
1212

13-
public class LinkedList<T> {
13+
public final class LinkedList<T> {
1414
public typealias Node = LinkedListNode<T>
1515

1616
fileprivate var head: Node?
@@ -187,14 +187,22 @@ extension LinkedList {
187187
}
188188
}
189189

190-
extension LinkedList {
190+
extension LinkedList: ExpressibleByArrayLiteral {
191191
convenience init(array: Array<T>) {
192192
self.init()
193193

194194
for element in array {
195195
self.append(element)
196196
}
197197
}
198+
199+
public convenience init(arrayLiteral elements: T...) {
200+
self.init()
201+
202+
for element in elements {
203+
self.append(element)
204+
}
205+
}
198206
}
199207

200208
let list = LinkedList<String>()
@@ -255,3 +263,13 @@ list[0] // "Swift"
255263

256264
list.remove(atIndex: 0) // "Swift"
257265
list.count // 0
266+
267+
let linkedList: LinkedList<Int> = [1, 2, 3, 4] // [1, 2, 3, 4]
268+
linkedList.count // 4
269+
linkedList[0] // 1
270+
271+
// Infer the type from the array
272+
let listArrayLiteral2: LinkedList = ["Swift", "Algorithm", "Club"]
273+
listArrayLiteral2.count // 3
274+
listArrayLiteral2[0] // "Swift"
275+
listArrayLiteral2.removeLast() // "Club"

Linked List/LinkedList.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class LinkedListNode<T> {
88
}
99
}
1010

11-
public class LinkedList<T> {
11+
public final class LinkedList<T>: ExpressibleByArrayLiteral {
1212
public typealias Node = LinkedListNode<T>
1313

1414
fileprivate var head: Node?
@@ -203,4 +203,12 @@ extension LinkedList {
203203
self.append(element)
204204
}
205205
}
206+
207+
public convenience init(arrayLiteral elements: T...) {
208+
self.init()
209+
210+
for element in elements {
211+
self.append(element)
212+
}
213+
}
206214
}

Linked List/Tests/LinkedListTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,26 @@ class LinkedListTest: XCTestCase {
248248
XCTAssertTrue(last === list.first)
249249
XCTAssertEqual(nodeCount, list.count)
250250
}
251+
252+
func testArrayLiteralInitTypeInfer() {
253+
let arrayLiteralInitInfer: LinkedList = [1.0, 2.0, 3.0]
254+
255+
XCTAssertEqual(arrayLiteralInitInfer.count, 3)
256+
XCTAssertEqual(arrayLiteralInitInfer.first?.value, 1.0)
257+
XCTAssertEqual(arrayLiteralInitInfer.last?.value, 3.0)
258+
XCTAssertEqual(arrayLiteralInitInfer[1], 2.0)
259+
XCTAssertEqual(arrayLiteralInitInfer.removeLast(), 3.0)
260+
XCTAssertEqual(arrayLiteralInitInfer.count, 2)
261+
}
262+
263+
func testArrayLiteralInitExplicit() {
264+
let arrayLiteralInitExplicit: LinkedList<Int> = [1, 2, 3]
265+
266+
XCTAssertEqual(arrayLiteralInitExplicit.count, 3)
267+
XCTAssertEqual(arrayLiteralInitExplicit.first?.value, 1)
268+
XCTAssertEqual(arrayLiteralInitExplicit.last?.value, 3)
269+
XCTAssertEqual(arrayLiteralInitExplicit[1], 2)
270+
XCTAssertEqual(arrayLiteralInitExplicit.removeLast(), 3)
271+
XCTAssertEqual(arrayLiteralInitExplicit.count, 2)
272+
}
251273
}

0 commit comments

Comments
 (0)