This file contains hidden or 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
import Foundation | |
// A bunch of convenience things | |
func const <A, B> (b: B) -> A -> B { | |
return { _ in b } | |
} | |
func repeat <A> (n: Int) -> A -> [A] { | |
return { a in | |
return map(Array(1...n), const(a)) | |
} |
This file contains hidden or 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
func minmax (a: Int, b: Int) -> (min: Int, max: Int) { | |
return (min(a, b), max(a, b)) | |
} | |
minmax(10, -2) //=> (.0 -2, .1 10) | |
minmax(10, -2).min //=> -2 | |
minmax(10, -2).max //=> 10 |
This file contains hidden or 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
func unit <A, B> (f: A -> () -> B) -> (A -> B) { | |
return { a in | |
return f(a)() | |
} | |
} |
This file contains hidden or 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
/*: | |
Brandon Williams | |
# Lenses in Swift | |
*/ | |
/*: | |
## i.e. Functional getters and setters |
This file contains hidden or 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
protocol LensType { | |
typealias Whole | |
typealias Part | |
var get: Whole -> Part { get } | |
var set: (Part, Whole) -> Whole { get } | |
} | |
struct Lens <A, B> : LensType { | |
typealias Whole = A |
This file contains hidden or 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
struct Lens<A, B> { | |
let get: A -> B | |
let set: (B, A) -> A | |
func map <C> (f: B -> C) -> Lens<A, C> { | |
return Lens( | |
get: { f(get($0)) }, | |
set: { set(f($0), $1) } | |
) | |
} |
This file contains hidden or 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
// In Parent.swift | |
struct Parent { | |
} | |
// In Child.swift | |
extension Parent { | |
struct Child { | |
} | |
} |
This file contains hidden or 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
struct F<A, B> { | |
let f: (A) -> B | |
} | |
typealias G<A> = F<A, Int> | |
extension G { | |
func something(a: A) -> Int { | |
return self.f(a) | |
// ^--- error: cannot convert return expression of type 'B' to return type 'Int' |
This file contains hidden or 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
class Function<B> { | |
func call<A>(_ a: A) -> B { | |
fatalError("unimplemented") | |
} | |
} | |
final class PrintInt: Function<Void> { | |
func call(_ a: Int) -> Void { | |
print(a) | |
} |
This file contains hidden or 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
class Function<B> { | |
func call<A>(_ a: A) -> B { | |
fatalError("unimplemented") | |
} | |
} | |
final class PrintInt: Function<Void> { | |
func call(_ a: Int) -> Void { | |
print(a) | |
} |