-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
120 lines (88 loc) · 2.17 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import UIKit
// Objectives
/*
- inout function
- generics, generic function
- stack: using a generic type
*/
// inout function allow us to change variables outside
// the scope of a function
// *****in-place means our function should be an inout function****
func swapTwoInts(int1: inout Int, int2: inout Int) {
let temp = int1 // saved the value of int1 to avoid overriden it
int1 = int2
int2 = temp
}
var num1 = 4
var num2 = 13
print("values of ints before swap is \(num1) \(num2)")
swapTwoInts(int1: &num1, int2: &num2)
print("values of ints after swap is \(num1) \(num2)")
func swapTwoStrings() {
}
func swapTwoFellows() {
}
// generic function
func swapTwoValues<T>(value1: inout T, value2: inout T) {
let temp = value1
value1 = value2
value2 = temp
}
var name1 = "Alex"
var name2 = "Paul"
print("values of swap before \(name1) \(name2)")
swapTwoValues(value1: &name1, value2: &name2)
print("values of swap after \(name1) \(name2)")
// Any is not type-safe
// Generic enforces type-safety
var arr: [Any] = [45, 7, 0, true, 90.8]
arr.append("fellow")
// implement a Generic Stack using an array
struct IntStack {
private var elements = [Int]()
}
// generic type
struct Stack<T> {
private var elements = [T]() // works with any type
public var peek: T? {
return elements.last
}
public mutating func push(item: T) {
elements.append(item)
}
public mutating func pop() -> T? {
guard !elements.isEmpty else {
return nil
}
return elements.removeLast()
}
}
var cohorts = Stack<Double>()
cohorts.push(item: 1.0)
cohorts.push(item: 2.0)
cohorts.push(item: 3.0)
cohorts.push(item: 4.0)
cohorts.push(item: 5.0)
cohorts.push(item: 6.3)
print(cohorts.peek) // 6.3
cohorts.pop()
cohorts.pop()
cohorts.pop()
cohorts.pop()
cohorts.pop()
cohorts.pop()
cohorts.pop()
cohorts.pop()
cohorts.pop()
cohorts.pop()
print(cohorts.peek) // 4.0
struct Fellow {
let name: String
let language: String
let isEmployed: Bool
}
let ameni = Fellow(name: "Ameni", language: "Swift", isEmployed: true)
let tom = Fellow(name: "Tom", language: "Kotlin", isEmployed: false)
var fellows = Stack<Fellow>()
fellows.push(item: tom)
print(fellows) // ["Tom"]