-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22dfa78
commit 18babca
Showing
6 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## [Point-Free](https://www.pointfree.co) | ||
|
||
> #### This directory contains code from Point-Free Episode: [Protocol Witnesses: Part 1](https://www.pointfree.co/episodes/ep33-protocol-witnesses-part-1) | ||
> | ||
> Protocols are a great tool for abstraction, but aren’t the only one. This week we begin to explore the tradeoffs of using protocols by highlighting a few areas in which they fall short in order to demonstrate how we can recover from these problems using a different tool and different tradeoffs. |
10 changes: 10 additions & 0 deletions
10
0033-protocol-witnesses-pt1/Witness.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
0033-protocol-witnesses-pt1/Witness.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
155 changes: 155 additions & 0 deletions
155
0033-protocol-witnesses-pt1/Witnesses.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
|
||
protocol Describable { | ||
var describe: String { get } | ||
} | ||
|
||
struct PostgresConnInfo { | ||
var database: String | ||
var hostname: String | ||
var password: String | ||
var port: Int | ||
var user: String | ||
} | ||
|
||
let localhostPostgres = PostgresConnInfo( | ||
database: "pointfreeco_development", | ||
hostname: "localhost", | ||
password: "", | ||
port: 5432, | ||
user: "pointfreeco" | ||
) | ||
|
||
//extension PostgresConnInfo: Describable { | ||
// var describe: String { | ||
// return "PostgresConnInfo(database: \"\(self.database)\", hostname: \"\(self.hostname)\", password: \"\(self.password)\", port: \"\(self.port)\", user: \"\(self.user)\")" | ||
// } | ||
//} | ||
|
||
//extension PostgresConnInfo: Describable { | ||
// var describe: String { | ||
// return """ | ||
//PostgresConnInfo( | ||
// database: \"\(self.database)\", | ||
// hostname: \"\(self.hostname)\", | ||
// password: \"\(self.password)\", | ||
// port: \"\(self.port)\", | ||
// user: \"\(self.user)\" | ||
//) | ||
//""" | ||
// } | ||
//} | ||
|
||
extension PostgresConnInfo: Describable { | ||
var describe: String { | ||
return "postgres://\(self.user):\(self.password)@\(self.hostname):\(self.port)/\(self.database)" | ||
} | ||
} | ||
|
||
print(localhostPostgres.describe) | ||
|
||
extension Int: Describable { | ||
var describe: String { | ||
return "\(self)" | ||
} | ||
} | ||
|
||
2.describe | ||
|
||
protocol EmptyInitializable { | ||
init() | ||
} | ||
|
||
extension String: EmptyInitializable { | ||
} | ||
extension Array: EmptyInitializable { | ||
} | ||
extension Int: EmptyInitializable { | ||
init() { | ||
self = 1 | ||
} | ||
} | ||
extension Optional: EmptyInitializable { | ||
init() { | ||
self = nil | ||
} | ||
} | ||
|
||
[1, 2, 3].reduce(0, +) | ||
|
||
extension Array { | ||
func reduce<Result: EmptyInitializable>(_ accumulation: (Result, Element) -> Result) -> Result { | ||
return self.reduce(Result(), accumulation) | ||
} | ||
} | ||
|
||
[1, 2, 3].reduce(+) | ||
[[1, 2], [], [3, 4]].reduce(+) | ||
["Hello", " ", "Blob"].reduce(+) | ||
|
||
protocol Combinable { | ||
func combine(with other: Self) -> Self | ||
} | ||
|
||
struct Combining<A> { | ||
let combine: (A, A) -> A | ||
} | ||
|
||
extension Int: Combinable { | ||
func combine(with other: Int) -> Int { | ||
return self * other | ||
} | ||
} | ||
extension String: Combinable { | ||
func combine(with other: String) -> String { | ||
return self + other | ||
} | ||
} | ||
extension Array: Combinable { | ||
func combine(with other: Array) -> Array { | ||
return self + other | ||
} | ||
} | ||
extension Optional: Combinable { | ||
func combine(with other: Optional) -> Optional { | ||
return self ?? other | ||
} | ||
} | ||
|
||
extension Array where Element: Combinable { | ||
func reduce(_ initial: Element) -> Element { | ||
return self.reduce(initial) { $0.combine(with: $1) } | ||
} | ||
} | ||
|
||
extension Array /* where Element: Combinable */ { | ||
func reduce(_ initial: Element, _ combining: Combining<Element>) -> Element { | ||
return self.reduce(initial, combining.combine) | ||
} | ||
} | ||
|
||
[1, 2, 3].reduce(1) | ||
[[1, 2], [], [3, 4]].reduce([]) | ||
[nil, nil, 3].reduce(nil) | ||
|
||
let sum = Combining<Int>(combine: +) | ||
[1, 2, 3, 4].reduce(0, sum) | ||
|
||
let product = Combining<Int>(combine: *) | ||
[1, 2, 3, 4].reduce(1, product) | ||
|
||
|
||
extension Array where Element: Combinable & EmptyInitializable { | ||
func reduce() -> Element { | ||
return self.reduce(Element()) { $0.combine(with: $1) } | ||
} | ||
} | ||
|
||
[1, 2, 3, 4].reduce() | ||
[[1, 2], [], [3, 4]].reduce() | ||
[nil, nil, 3].reduce() | ||
|
||
//extension Int: Combinable { | ||
// func combine(with other: Int) -> Int { | ||
// return self * other | ||
// } | ||
//} |
4 changes: 4 additions & 0 deletions
4
0033-protocol-witnesses-pt1/Witnesses.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters