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
711871ee
Commit
711871ee
authored
3 years ago
by
Peter J. Keleher
Browse files
Options
Downloads
Patches
Plain Diff
auto
parent
f4bf5964
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
03_demo.txt
+0
-103
0 additions, 103 deletions
03_demo.txt
demos/03_demo.md
+0
-98
0 additions, 98 deletions
demos/03_demo.md
exercises-9-9.md
+0
-185
0 additions, 185 deletions
exercises-9-9.md
with
0 additions
and
386 deletions
03_demo.txt
deleted
100644 → 0
+
0
−
103
View file @
f4bf5964
// Notes used by pete in class (not originally intended to be public,
// but I was asked.....), some of these are examples of
// correct usage, and some illustrate issues to be fixed in class. Some
// lines are comments, some are notes to myself. They are not intended
// to be correct "as is". Use them to explore on your own.
```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.
demos/03_demo.md
deleted
100644 → 0
+
0
−
98
View file @
f4bf5964
```
swift
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
deleted
100644 → 0
+
0
−
185
View file @
f4bf5964
// Notes used by pete in class (not originally intended to be public,
// but I was asked.....), some of these are examples of
// correct usage, and some illustrate issues to be fixed in class. Some
// lines are comments, some are notes to myself. They are not intended
// to be correct "as is". Use them to explore on your own.
```
swift
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