Skip to content

Commit 056795e

Browse files
author
Chris Pilcher
authored
Merge pull request kodecocodes#145 from chris-pilcher/simplify-stack-pop
Fix for kodecocodes#144 stack pop function is unnecessarily complex
2 parents 06f2160 + 9f62cf3 commit 056795e

File tree

4 files changed

+4
-22
lines changed

4 files changed

+4
-22
lines changed

Stack/README.markdown

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ public struct Stack<T> {
5757
}
5858

5959
public mutating func pop() -> T? {
60-
if isEmpty {
61-
return nil
62-
} else {
63-
return array.removeLast()
64-
}
60+
return array.popLast()
6561
}
6662

6763
public func peek() -> T? {

Stack/Stack.playground/Contents.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ public struct Stack<T> {
2727
}
2828

2929
public mutating func pop() -> T? {
30-
if isEmpty {
31-
return nil
32-
} else {
33-
return array.removeLast()
34-
}
30+
return array.popLast()
3531
}
3632

3733
public func peek() -> T? {

Stack/Stack.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

Stack/Stack.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ public struct Stack<T> {
1919
}
2020

2121
public mutating func pop() -> T? {
22-
if isEmpty {
23-
return nil
24-
} else {
25-
return array.removeLast()
26-
}
22+
return array.popLast()
2723
}
2824

2925
public func peek() -> T? {
@@ -34,7 +30,7 @@ public struct Stack<T> {
3430
extension Stack: SequenceType {
3531
public func generate() -> AnyGenerator<T> {
3632
var curr = self
37-
return anyGenerator {
33+
return AnyGenerator {
3834
_ -> T? in
3935
return curr.pop()
4036
}

0 commit comments

Comments
 (0)