From 027e23e8b2d9221fcbc24cc6a4ec8725069a868e Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Sun, 24 Mar 2019 23:08:21 -0400 Subject: [PATCH] 51 --- .../Contents.swift" | 162 ++++++++++++++++++ .../contents.xcplayground" | 4 + .../README.md" | 5 + README.md | 2 + 4 files changed, 173 insertions(+) create mode 100644 "0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/Contents.swift" create mode 100644 "0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/contents.xcplayground" create mode 100644 "0051-structs-\360\237\244\235-enums/README.md" diff --git "a/0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/Contents.swift" "b/0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/Contents.swift" new file mode 100644 index 00000000..00ea71cb --- /dev/null +++ "b/0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/Contents.swift" @@ -0,0 +1,162 @@ + +struct Pair { + let first: A + let second: B +} + +Pair.init(first: (), second: ()) + +Pair.init(first: true, second: ()) +Pair.init(first: false, second: ()) + +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, two, three } +Pair.init(first: true, second: .one) +Pair.init(first: false, second: .one) +Pair.init(first: true, second: .two) +Pair.init(first: false, second: .two) +Pair.init(first: true, second: .three) +Pair.init(first: false, second: .three) + +//Pair.init(first: true, second: <#T##Never#>) + +enum Either { + case left(A) + case right(B) +} + +Either.left(()) +Either.right(()) + +Either.left(true) +Either.left(false) +Either.right(()) + +Either.left(true) +Either.left(false) +Either.right(true) +Either.right(false) + +Either.left(true) +Either.left(false) +Either.right(.one) +Either.right(.two) +Either.right(.three) + +Either.left(true) +Either.left(false) +//Either.right(???) + + +struct User { + let id: Int + let isAdmin: Bool + let name: String +} + +User.init + +[1, 2, 3, 4].map(String.init) + +["1", "2", "blob", "3"].compactMap(Int.init) + +Either.left +Either.right + +[1, 2, 3, 4].map(Either.left) + +let value = Either.left(42) +switch value { +case let .left(left): + print(left) +case let .right(right): + print(right) +} + +func compute(_ xs: [Int]) -> (product: Int, sum: Int, average: Double) { + var product = 1 + var sum = 0 + xs.forEach { x in + product *= x + sum += x + } + return (product, sum, Double(sum) / Double(xs.count)) +} + +let result = compute([1, 2, 3, 4, 5]) +result.product +result.sum + +let (product, sum, average) = compute([1, 2, 3, 4, 5]) +product +sum +average + + +let tuple: (id: Int, name: String) = (42, "Blob") + +[1, 2, 3].map { $0 + 1 } + +tuple.0 +tuple.1 +tuple.id +tuple.name + +[1, 2, 3, 4, 5].reduce((product: 1, sum: 0)) { accum, x in + (accum.product * x, accum.sum + x) +} + +3 * 4 +//3 + 4 + +//3 `addThreeToFour` 4 + + +let _: (Int, String) + +let choice: (Int | String) = .0(42) +let choice: (Int | String) = .1("Blob") + +let choice: (id: Int | param: String) = .id(42) +let choice: (id: Int | param: String) = .param("Blob") + +switch choice { +case let .0(id): + print(id) +case let .1(param): + print(param) +} + +switch choice { +case let .id(id): + print(id) +case let .param(param): + print(param) +} + +//enum Optional { +// case some(A) +// case none +//} + +enum Loading { + case some(A) + case loading +} + +enum EmptyCase { + case some(A) + case emptyState +} + +//Loading> + +func render(data: (user: [User] | empty | loading | )) { + switch data { + + } +} diff --git "a/0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/contents.xcplayground" "b/0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/contents.xcplayground" new file mode 100644 index 00000000..63b6dd8d --- /dev/null +++ "b/0051-structs-\360\237\244\235-enums/Enums\360\237\244\235Structs.playground/contents.xcplayground" @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git "a/0051-structs-\360\237\244\235-enums/README.md" "b/0051-structs-\360\237\244\235-enums/README.md" new file mode 100644 index 00000000..eaa79bfb --- /dev/null +++ "b/0051-structs-\360\237\244\235-enums/README.md" @@ -0,0 +1,5 @@ +## [Point-Free](https://www.pointfree.co) + +> #### This directory contains code from Point-Free Episode: [Structs 🤝 Enums](https://www.pointfree.co/episodes/ep51-structs-enums) +> +> Name a more iconic duo... We'll wait. Structs and enums go together like peanut butter and jelly, or multiplication and addition. One's no more important than the other: they're completely complementary. This week we'll explore how features on one may surprisingly manifest themselves on the other. diff --git a/README.md b/README.md index 253998d9..70a680f5 100644 --- a/README.md +++ b/README.md @@ -52,3 +52,5 @@ This repository is the home of code written on episodes of 1. [Predictable Randomness: Part 1](0047-predictable-randomness-pt1) 1. [Predictable Randomness: Part 2](0048-predictable-randomness-pt2) 1. [Generative Art: Part 1](0049-generative-art-pt1) +1. [Generative Art: Part 2](0050-generative-art-pt2) +1. [Structs 🤝 Enums](0051-structs-🤝-enums)