-
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
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
0006-functional-setters/Functional Setters.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,110 @@ | ||
|
||
let pair = (42, "Swift") | ||
|
||
(incr(pair.0), pair.1) | ||
|
||
func incrFirst<A>(_ pair: (Int, A)) -> (Int, A) { | ||
return (incr(pair.0), pair.1) | ||
} | ||
|
||
incrFirst(pair) | ||
|
||
func first<A, B, C>(_ f: @escaping (A) -> C) -> ((A, B)) -> (C, B) { | ||
return { pair in | ||
(f(pair.0), pair.1) | ||
} | ||
} | ||
|
||
pair | ||
|> first(incr) | ||
|> first(String.init) | ||
|
||
func second<A, B, C>(_ f: @escaping (B) -> C) -> ((A, B)) -> (A, C) { | ||
return { pair in | ||
(pair.0, f(pair.1)) | ||
} | ||
} | ||
|
||
pair | ||
|> first(incr) | ||
|> first(String.init) | ||
|> second { $0 + "!" } | ||
|
||
|
||
pair | ||
|> first(incr) | ||
|> first(String.init) | ||
|> second(zurry(flip(String.uppercased))) | ||
|
||
pair | ||
|> first(incr >>> String.init) | ||
|> second(zurry(flip(String.uppercased))) | ||
|
||
first(incr) | ||
>>> first(String.init) | ||
>>> second(zurry(flip(String.uppercased))) | ||
|
||
var copyPair = pair | ||
copyPair.0 += 1 | ||
//copyPair.0 = String(copyPair.0) | ||
copyPair.1 = copyPair.1.uppercased() | ||
|
||
let nested = ((1, true), "Swift") | ||
|
||
nested | ||
|> first { $0 |> second { !$0 } } | ||
|
||
nested | ||
|> (second >>> first) { !$0 } | ||
|
||
precedencegroup BackwardsComposition { | ||
associativity: left | ||
} | ||
infix operator <<<: BackwardsComposition | ||
func <<< <A, B, C>(_ f: @escaping (B) -> C, _ g: @escaping (A) -> B) -> (A) -> C { | ||
return { f(g($0)) } | ||
} | ||
|
||
nested | ||
|> (first <<< second) { !$0 } | ||
|
||
nested | ||
|> (first <<< first)(incr) | ||
|> (first <<< second) { !$0 } | ||
|> second { $0 + "!" } | ||
|
||
//let transformation = (first <<< first)(incr) | ||
// <> (first <<< second) { !$0 } | ||
// <> second { $0 + "!" } | ||
// | ||
//nested |> transformation | ||
|
||
// ((A) -> B) -> (S) -> T | ||
|
||
// ((A) -> B) -> ((A, C)) -> (B, C) | ||
// ((A) -> B) -> ((C, A)) -> (C, B) | ||
|
||
// ((A) -> B) -> ([A]) -> [B] | ||
|
||
public func map<A, B>(_ f: @escaping (A) -> B) -> ([A]) -> [B] { | ||
return { xs in xs.map(f) } | ||
} | ||
|
||
(42, ["Swift", "Objective-C"]) | ||
|> (second <<< map) { $0 + "!" } | ||
|> first(incr) | ||
|
||
dump( | ||
[(42, ["Swift", "Objective-C"]), (1729, ["Haskell", "Purescript"])] | ||
|> (map <<< second <<< map) { $0 + "!" } | ||
) | ||
|
||
// | ||
|
||
let data = [(42, ["Swift", "Objective-C"]), (1729, ["Haskell", "Purescript"])] | ||
|
||
data | ||
.map { ($0.0, $0.1.map { $0 + "!" }) } | ||
|
||
|
||
|
54 changes: 54 additions & 0 deletions
54
0006-functional-setters/Functional Setters.playground/Sources/Utils.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,54 @@ | ||
precedencegroup ForwardApplication { | ||
associativity: left | ||
} | ||
|
||
infix operator |>: ForwardApplication | ||
|
||
precedencegroup ForwardComposition { | ||
associativity: left | ||
higherThan: ForwardApplication | ||
} | ||
|
||
infix operator >>>: ForwardComposition | ||
|
||
public func |> <A, B>(x: A, f: (A) -> B) -> B { | ||
return f(x) | ||
} | ||
|
||
public func >>> <A, B, C>(f: @escaping (A) -> B, g: @escaping (B) -> C) -> (A) -> C { | ||
return { g(f($0)) } | ||
} | ||
|
||
public func zurry<A>(_ f: () -> A) -> A { | ||
return f() | ||
} | ||
|
||
public func flip<A, B, C>(_ f: @escaping (A) -> (B) -> C) -> (B) -> (A) -> C { | ||
|
||
return { b in { a in f(a)(b) } } | ||
} | ||
|
||
public func flip<A, C>(_ f: @escaping (A) -> () -> C) -> () -> (A) -> C { | ||
|
||
return { { a in f(a)() } } | ||
} | ||
|
||
|
||
precedencegroup SingleTypeComposition { | ||
associativity: left | ||
higherThan: ForwardApplication | ||
} | ||
|
||
infix operator <>: SingleTypeComposition | ||
|
||
public func <> <A>(f: @escaping (A) -> A, g: @escaping (A) -> A) -> (A) -> A { | ||
return f >>> g | ||
} | ||
|
||
public func incr(_ x: Int) -> Int { | ||
return x + 1 | ||
} | ||
|
||
public func square(_ x: Int) -> Int { | ||
return x * x | ||
} |
4 changes: 4 additions & 0 deletions
4
0006-functional-setters/Functional Setters.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
### [Point-Free](https://www.pointfree.co) Episode #6 | ||
|
||
### Functional Setters | ||
|
||
> The programs we write can be reduced to transforming data from one form into another. We’re used to transforming this data imperatively, with setters. There’s a strange world of composition hiding here in plain sight, and it has a surprising link to a familiar functional friend. | ||
This directory contains code from Point-Free Episode #6: | ||
[Functional Setters](https://www.pointfree.co/episodes/ep6-functional-setters) |