From 5efbc1d1031b814bc550ec3685423d0b18b5fbea Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Sun, 18 Feb 2018 20:00:15 -0500 Subject: [PATCH] Added playground --- .../Contents.swift | 200 ++++++++++++++++++ .../contents.xcplayground | 4 + 2 files changed, 204 insertions(+) create mode 100644 0004-adt/Algebraic Data Types.playground/Contents.swift create mode 100644 0004-adt/Algebraic Data Types.playground/contents.xcplayground diff --git a/0004-adt/Algebraic Data Types.playground/Contents.swift b/0004-adt/Algebraic Data Types.playground/Contents.swift new file mode 100644 index 00000000..61e17b31 --- /dev/null +++ b/0004-adt/Algebraic Data Types.playground/Contents.swift @@ -0,0 +1,200 @@ + +struct Pair { + let first: A + let second: B +} + +Pair.init(first: true, second: true) +Pair.init(first: true, second: false) +Pair.init(first: false, second: true) +Pair.init(first: false, second: false) + +enum Three { + case one + case two + case three +} + +Pair.init(first: true, second: .one) +Pair.init(first: true, second: .two) +Pair.init(first: true, second: .three) +Pair.init(first: false, second: .one) +Pair.init(first: false, second: .two) +Pair.init(first: false, second: .three) + +let _: Void = Void() +let _: Void = () +let _: () = () + +func foo(_ x: Int) /* -> Void */ { + // return () +} + +Pair.init(first: true, second: ()) +Pair.init(first: false, second: ()) + +Pair.init(first: (), second: ()) + +enum Never {} + +//let _: Never = ??? + +//Pair.init(first: true, second: ???) + + +// Pair = 4 = 2 * 2 +// Pair = 6 = 2 * 3 +// Pair = 2 = 2 * 1 +// Pair = 1 = 1 * 1 +// Pair = 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 +// Pair = Bool * Bool +// Pair = Bool * Three +// Pair = Bool * Void +// Pair = Bool * Never + +// Pair = Bool * String +// String * [Int] +// [String] * [[Int]] +// Never = 0 +// Void = 1 +// Bool = 2 +// 2 * String + +enum Either { + case left(A) + case right(B) +} + +Either.left(true) +Either.left(false) +Either.right(true) +Either.right(false) +// Either = 4 = 2 + 2 +// 2 + 2 + +Either.left(true) +Either.left(false) +Either.right(.one) +Either.right(.two) +Either.right(.three) +// Either = 5 = 2 + 3 +// Bool + Three +// 2 + Three + +Either.left(true) +Either.left(false) +Either.right(()) +// Either = 3 = 2 + 1 +// 2 + 1 + +Either.left(true) +Either.left(false) +//Either.right(???) +// Either = 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 + +//Either, Pair> + +// A * B + A * C = A * (B + C) + +//Pair> + + +// Pair, Either> + +// (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, Error> +//Result<(Data, URLResponse), Error> +//Result +//Result? diff --git a/0004-adt/Algebraic Data Types.playground/contents.xcplayground b/0004-adt/Algebraic Data Types.playground/contents.xcplayground new file mode 100644 index 00000000..63b6dd8d --- /dev/null +++ b/0004-adt/Algebraic Data Types.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file