Skip to main content
Added another simplification.
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

It also is not necessary to make the head and node properties of NodeChain implicitly unwrapped optionals (and does not make much sense for constant properties anyway). Simple non-optional constant properties will do:

    let head: Node<Element>
    let tail: Node<Element>
var l1 = LinkedList([1, 2, 3])
let l2 = l1
_ = l1.popFirstremoveFirst()
_ = l1.popLastremoveLast()
var l1 = LinkedList([1, 2, 3])
let l2 = l1
_ = l1.popFirst()
_ = l1.popLast()

It also is not necessary to make the head and node properties of NodeChain implicitly unwrapped optionals (and does not make much sense for constant properties anyway). Simple non-optional constant properties will do:

    let head: Node<Element>
    let tail: Node<Element>
var l1 = LinkedList([1, 2, 3])
let l2 = l1
l1.removeFirst()
l1.removeLast()
Remove the suggestion to add @discardableResult to popFirst/Last(). As @ielyamani noticed, this is not how the Standard Library defines those methods, and removeFirst/Last() can be used instead if the result is ignored.
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

Some return values can be ignored

If you add the @discardableResult attribute to the popFirst() and popLast() method then a user of the library has the option to remove elements

var l1 = LinkedList([1, 2, 3])
l1.popFirst()
l1.popLast()

without getting a “Result of call to ... is unused” warning.

Performance

Some return values can be ignored

If you add the @discardableResult attribute to the popFirst() and popLast() method then a user of the library has the option to remove elements

var l1 = LinkedList([1, 2, 3])
l1.popFirst()
l1.popLast()

without getting a “Result of call to ... is unused” warning.

Performance

Performance

added 144 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

can be simplifiedshortened to

So these nested type do not need to be generic: They can simply use the Element type of of LinkedList, i.e.

The same applies to the other nested generic types listed above. This allows to get rid of the distracting <T> placeholders and some type aliases. It becomes obvious that the same element type is used everywhere.

You have nicenicely structured the code by using separate extensions for the various protocol conformances.

(This paragraph is surely opinion-based.) The guard statement was introduced to get rid of the “if-let pyramid of doom,” it allows to unwrap a variable without introducing another scope/indentation level.

can be simplified to

So these nested type do not need to be generic: They can simply use the Element type of of LinkedList, i.e.

The same applies to the other nested generic types listed above.

You have nice structured the code by using separate extensions for the various protocol conformances.

(This paragraph is surely opinion-based.) The guard statement was introduced to get rid of the “if-let pyramid of doom,” it allows to unwrap a variable without introducing another scope level.

can be shortened to

So these nested type do not need to be generic: They can simply use the Element type of LinkedList, i.e.

The same applies to the other nested generic types listed above. This allows to get rid of the distracting <T> placeholders and some type aliases. It becomes obvious that the same element type is used everywhere.

You have nicely structured the code by using separate extensions for the various protocol conformances.

(This paragraph is surely opinion-based.) The guard statement was introduced to get rid of the “if-let pyramid of doom,” it allows to unwrap a variable without introducing another scope/indentation level.

added 415 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading