Skip to content

Commit

Permalink
51
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Mar 25, 2019
1 parent 1c58c42 commit 027e23e
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
162 changes: 162 additions & 0 deletions 0051-structs-🤝-enums/Enums🤝Structs.playground/Contents.swift
@@ -0,0 +1,162 @@

struct Pair<A, B> {
let first: A
let second: B
}

Pair<Void, Void>.init(first: (), second: ())

Pair<Bool, Void>.init(first: true, second: ())
Pair<Bool, Void>.init(first: false, second: ())

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, two, three }
Pair<Bool, Three>.init(first: true, second: .one)
Pair<Bool, Three>.init(first: false, second: .one)
Pair<Bool, Three>.init(first: true, second: .two)
Pair<Bool, Three>.init(first: false, second: .two)
Pair<Bool, Three>.init(first: true, second: .three)
Pair<Bool, Three>.init(first: false, second: .three)

//Pair<Bool, Never>.init(first: true, second: <#T##Never#>)

enum Either<A, B> {
case left(A)
case right(B)
}

Either<Void, Void>.left(())
Either<Void, Void>.right(())

Either<Bool, Void>.left(true)
Either<Bool, Void>.left(false)
Either<Bool, Void>.right(())

Either<Bool, Bool>.left(true)
Either<Bool, Bool>.left(false)
Either<Bool, Bool>.right(true)
Either<Bool, Bool>.right(false)

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, Never>.left(true)
Either<Bool, Never>.left(false)
//Either<Bool, Never>.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<Int, String>.left
Either<Int, String>.right

[1, 2, 3, 4].map(Either<Int, String>.left)

let value = Either<Int, String>.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<A> {
// case some(A)
// case none
//}

enum Loading<A> {
case some(A)
case loading
}

enum EmptyCase<A> {
case some(A)
case emptyState
}

//Loading<EmptyCase<[User]>>

func render(data: (user: [User] | empty | loading | )) {
switch data {

}
}
@@ -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>
5 changes: 5 additions & 0 deletions 0051-structs-🤝-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.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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)

0 comments on commit 027e23e

Please sign in to comment.