Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved LinkedList: init from Sequence #955

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Linked List/LinkedList.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -272,12 +272,12 @@ extension LinkedList {
}
}

// MARK: - Extension to enable initialization from an Array
// MARK: - Extension to enable initialization from a Sequence
extension LinkedList {
convenience init(array: Array<T>) {
convenience init<S>(_ sequence: S) where S: Sequence, S.Element == T {
self.init()

array.forEach { append($0) }
sequence.forEach { append($0) }
}
}

6 changes: 3 additions & 3 deletions Linked List/LinkedList.swift
Original file line number Diff line number Diff line change
@@ -266,12 +266,12 @@ extension LinkedList {
}
}

// MARK: - Extension to enable initialization from an Array
// MARK: - Extension to enable initialization from a Sequence
extension LinkedList {
convenience init(array: Array<T>) {
convenience init<S>(_ sequence: S) where S: Sequence, S.Element == T {
self.init()

array.forEach { append($0) }
sequence.forEach { append($0) }
}
}

11 changes: 11 additions & 0 deletions Linked List/Tests/LinkedListTests.swift
Original file line number Diff line number Diff line change
@@ -307,6 +307,17 @@ class LinkedListTest: XCTestCase {
XCTAssertEqual(nodeCount, list.count)
}

func testSequenceInitTypeInfer() {
let arrayInitInfer = LinkedList([1.0, 2.0, 3.0])

XCTAssertEqual(arrayInitInfer.count, 3)
XCTAssertEqual(arrayInitInfer.head?.value, 1.0)
XCTAssertEqual(arrayInitInfer.last?.value, 3.0)
XCTAssertEqual(arrayInitInfer[1], 2.0)
XCTAssertEqual(arrayInitInfer.removeLast(), 3.0)
XCTAssertEqual(arrayInitInfer.count, 2)
}

func testArrayLiteralInitTypeInfer() {
let arrayLiteralInitInfer: LinkedList = [1.0, 2.0, 3.0]