Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cmsc436PublicF2021
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Peter Keleher
cmsc436PublicF2021
Commits
e1f30a04
Commit
e1f30a04
authored
3 years ago
by
Peter J. Keleher
Browse files
Options
Downloads
Patches
Plain Diff
auto
parent
77bb1a6f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
03_demo.txt
+97
-0
97 additions, 0 deletions
03_demo.txt
exercises-9-9.md
+177
-0
177 additions, 0 deletions
exercises-9-9.md
with
274 additions
and
0 deletions
03_demo.txt
0 → 100644
+
97
−
0
View file @
e1f30a04
```go
func changeB(a:String, b: inout String) {
b = "\(a)\(b)"
}
var s="this"
changeB(a:"changing ", b:&s)
print(s)
//=====================================================================
func outer() -> (Int) -> Int {
var incr = 0
func inner(_ a:Int) -> Int {
incr += 1
return a+incr
}
return inner
}
let x = outer()
x(33)
//=====================================================================
Array::sorted(by: (T,T)->Bool)
let nums = [7,5,14,12,16]
// Sort with inferred types
let snums = nums.sorted(by:{a,b in a>b})
// Sort with positional parameters
let snums2 = nums.sorted(by:{$0>$1})
// Sort with function name
let snums3 = nums.sorted(by:>)
// Sort with a trailing closure
let snums4 = nums.sorted() {$0>$1}
// Sort with parentheses omitted
let snums5 = nums.sorted {$0>$1}
//=====================================================================
enum Earthsea: CaseIterable {
case AWizardOfEarthsea
case TheTombsOfAtuan
case TheFarthestShore
case Tehanu
}
for book in Earthsea.allCases {
print(book)
}
//=====================================================================
struct Point {
var x: Int = 0
var y: Int = 0
}
let p = Point(x:3, y: 4)
let p2 = Point(x:4, y: 5)
enum Shape {
case circle(center:Point, radius:Double)
case rect(lowerLeft:Point, upperRight:Point)
}
let s = Shape.circle(center:p, radius:1.5)
switch s {
case .circle(let c, let r):
print("It's a circle \(c) \(r)")
case let .rect(ll, ur):
print("Just a rect \(ll) \(ur)")
default:
print("default")
}
let sr = Shape.rect(lowerLeft: p, upperRight: p2)
switch sr {
case .circle(let c, let r):
print("It's a circle \(c) \(r)")
case let .rect(ll, ur):
print("Just a rect \(ll) \(ur)")
default:
print("default")
}
```
This diff is collapsed.
Click to expand it.
exercises-9-9.md
0 → 100644
+
177
−
0
View file @
e1f30a04
struct Stack
<Element>
{
var items =
[
Element
](
)
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
}
var t = Stack
<Int>
()
print(t)
t.push(3)
t.push(4)
print(t.pop())
var s = Stack
<String>
()
s.push("nice")
print(s.pop())
//=====================================================================
extension Stack {
var top: T? {
return items.isEmpty ? nil : items[0]
}
}
if let x = t.top {
print("there is another int")
}
if let y = s.top {
print("there is another string")
}
//=====================================================================
enum MyError: Error {
case bad1
case bad2
}
func blah() throws {
defer { print("I've got a deferment") }
defer { print("Second deferment") }
throw MyError.bad1
print("regular blah")
}
do {
try blah()
} catch let e where e is MyError {
print("my bad
\(
e)")
}
//=====================================================================
class Person { // references: 0
let name:String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("
\(
name) is being deinitialized") }
}
class Apartment { // references: 0
let unit: String
init(unit: String) { self.unit = unit }
var tenant: Person?
deinit { print("Apartment
\(
unit) is being deinitialized") }
}
var john:Person? = Person(name: "John Doe")
var unit4A:Apartment? = Apartment(unit: "4A")
john!.apartment = unit4A
unit4A!.tenant = john
john = nil
unit4A = nil
// make either ref weak and they go away
// make .tenant unowned (.apartment strong)
john = nil
print(unit4A!.tenant) // crash
//=====================================================================
class Country {
let name: String
var capitalCity: City? = nil
init(nameIn: String, capitalName: String) {
name = nameIn
capitalCity = City(nameIn: capitalName, countryIn: self)
}
deinit {
print("
\(
name) deiniting")
}
}
class City {
let name: String
unowned let country: Country
init(nameIn: String, countryIn: Country) {
name = nameIn
country = countryIn
}
deinit {
print("
\(
name) deiniting")
}
}
var country :Country? = Country(nameIn: "Bangladesh", capitalName: "Dhaka")
// need to make capitalCity OPTIONAL so init can complete
// still have loop, so make country UNOWNED
//=====================================================================
class Person {
var firstName: String?
var lastName: String?
var fullName: ()->String = {
return ("
\(
self.firstName!)
\(
self.lastName!)")
}
init(f: String, l: String) {
firstName = f
lastName = l
}
deinit {
print("deiniting")
}
}
var x:Person? = Person(f: "pete", l: "keleher")
print(x!.fullName())
x = nil
print("done")
// Add 'lazy' to def of fullname, allow not violate safety check.
// add " [unowned self] in" (a "capture list") to fullName closure to avoid strong reference loop:
class Person {
var firstName: String?
var lastName: String?
lazy var fullName: ()->String = {
[unowned self]
return ("
\(
self.firstName!)
\(
self.lastName!)")
}
init(f: String, l: String) {
firstName = f
lastName = l
}
deinit {
print("deiniting")
}
}
//=====================================================================
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment