-
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
Showing
2 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
0004-adt/Algebraic Data Types.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,200 @@ | ||
|
||
struct Pair<A, B> { | ||
let first: A | ||
let second: B | ||
} | ||
|
||
Pair<Bool, Bool>.init(first: true, second: true) | ||
Pair<Bool, Bool>.init(first: true, second: false) | ||
Pair<Bool, Bool>.init(first: false, second: true) | ||
Pair<Bool, Bool>.init(first: false, second: false) | ||
|
||
enum Three { | ||
case one | ||
case two | ||
case three | ||
} | ||
|
||
Pair<Bool, Three>.init(first: true, second: .one) | ||
Pair<Bool, Three>.init(first: true, second: .two) | ||
Pair<Bool, Three>.init(first: true, second: .three) | ||
Pair<Bool, Three>.init(first: false, second: .one) | ||
Pair<Bool, Three>.init(first: false, second: .two) | ||
Pair<Bool, Three>.init(first: false, second: .three) | ||
|
||
let _: Void = Void() | ||
let _: Void = () | ||
let _: () = () | ||
|
||
func foo(_ x: Int) /* -> Void */ { | ||
// return () | ||
} | ||
|
||
Pair<Bool, Void>.init(first: true, second: ()) | ||
Pair<Bool, Void>.init(first: false, second: ()) | ||
|
||
Pair<Void, Void>.init(first: (), second: ()) | ||
|
||
enum Never {} | ||
|
||
//let _: Never = ??? | ||
|
||
//Pair<Bool, Never>.init(first: true, second: ???) | ||
|
||
|
||
// Pair<Bool, Bool> = 4 = 2 * 2 | ||
// Pair<Bool, Three> = 6 = 2 * 3 | ||
// Pair<Bool, Void> = 2 = 2 * 1 | ||
// Pair<Void, Void> = 1 = 1 * 1 | ||
// Pair<Bool, Never> = 0 = 2 * 0 | ||
|
||
|
||
enum Theme { | ||
case light | ||
case dark | ||
} | ||
|
||
enum State { | ||
case highlighted | ||
case normal | ||
case selected | ||
} | ||
|
||
struct Component { | ||
let enabled: Bool | ||
let state: State | ||
let theme: Theme | ||
} | ||
|
||
// 2 * 3 * 2 = 12 | ||
|
||
// Pair<A, B> = A * B | ||
// Pair<Bool, Bool> = Bool * Bool | ||
// Pair<Bool, Three> = Bool * Three | ||
// Pair<Bool, Void> = Bool * Void | ||
// Pair<Bool, Never> = Bool * Never | ||
|
||
// Pair<Bool, String> = Bool * String | ||
// String * [Int] | ||
// [String] * [[Int]] | ||
// Never = 0 | ||
// Void = 1 | ||
// Bool = 2 | ||
// 2 * String | ||
|
||
enum Either<A, B> { | ||
case left(A) | ||
case right(B) | ||
} | ||
|
||
Either<Bool, Bool>.left(true) | ||
Either<Bool, Bool>.left(false) | ||
Either<Bool, Bool>.right(true) | ||
Either<Bool, Bool>.right(false) | ||
// Either<Bool, Bool> = 4 = 2 + 2 | ||
// 2 + 2 | ||
|
||
Either<Bool, Three>.left(true) | ||
Either<Bool, Three>.left(false) | ||
Either<Bool, Three>.right(.one) | ||
Either<Bool, Three>.right(.two) | ||
Either<Bool, Three>.right(.three) | ||
// Either<Bool, Three> = 5 = 2 + 3 | ||
// Bool + Three | ||
// 2 + Three | ||
|
||
Either<Bool, Void>.left(true) | ||
Either<Bool, Void>.left(false) | ||
Either<Bool, Void>.right(()) | ||
// Either<Bool, Void> = 3 = 2 + 1 | ||
// 2 + 1 | ||
|
||
Either<Bool, Never>.left(true) | ||
Either<Bool, Never>.left(false) | ||
//Either<Bool, Never>.right(???) | ||
// Either<Bool, Never> = 2 = 2 + 0 | ||
// 2 + 0 | ||
|
||
struct Unit {} | ||
// enum Never {} | ||
|
||
let unit = Unit() | ||
|
||
extension Unit: Equatable { | ||
static func == (lhs: Unit, rhs: Unit) -> Bool { | ||
return true | ||
} | ||
} | ||
|
||
//extension Void {} | ||
|
||
|
||
|
||
func sum(_ xs: [Int]) -> Int { | ||
var result: Int = 0 | ||
for x in xs { | ||
result += x | ||
} | ||
return result | ||
} | ||
|
||
func product(_ xs: [Int]) -> Int { | ||
var result: Int = 1 | ||
for x in xs { | ||
result *= x | ||
} | ||
return result | ||
} | ||
|
||
let xs = [Int]() | ||
sum(xs) | ||
product(xs) | ||
|
||
sum([1, 2]) + 0 == sum([1, 2]) | ||
product([1, 2]) * 1 == product([1, 2]) | ||
|
||
// Void = 1 | ||
// A * 1 = A = 1 * A | ||
|
||
// Never = 0 | ||
// A * 0 = 0 = 0 * A | ||
|
||
// A + 0 = A = 0 + A | ||
|
||
// A + 1 = 1 + A = A? | ||
|
||
//Either<A, Void> | ||
|
||
//Either<Pair<A, B>, Pair<A, C>> | ||
|
||
// A * B + A * C = A * (B + C) | ||
|
||
//Pair<A, Either<B, C>> | ||
|
||
|
||
// Pair<Either<A, B>, Either<A, C>> | ||
|
||
// (A + B) * (A + C) | ||
// A * A + A * C + B * A + B * C | ||
|
||
import Foundation | ||
|
||
//URLSession.shared | ||
// .dataTask(with: <#T##URL#>, completionHandler: <#T##(Data?, URLResponse?, Error?) -> Void#>) | ||
|
||
// (Data + 1) * (URLResponse + 1) * (Error + 1) | ||
// = Data * URLResponse * Error | ||
// + Data * URLResponse | ||
// + URLResponse * Error | ||
// + Data * Error | ||
// + Data | ||
// + URLResponse | ||
// + Error | ||
// + 1 | ||
|
||
// Data * URLResponse + Error | ||
|
||
//Either<Pair<Data, URLResponse>, Error> | ||
//Result<(Data, URLResponse), Error> | ||
//Result<Date, Never> | ||
//Result<A, Error>? |
4 changes: 4 additions & 0 deletions
4
0004-adt/Algebraic Data Types.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> |