Created
March 14, 2018 14:49
-
-
Save klaaspieter/f3a18b5e32cddbd083c403b7fdba355e to your computer and use it in GitHub Desktop.
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
precedencegroup ForwardApplication { | |
associativity: left | |
} | |
infix operator |>: ForwardApplication | |
public func |> <A, B>(x: A, f: (A) -> B) -> B { | |
return f(x) | |
} | |
precedencegroup SingleTypeComposition { | |
associativity: right | |
higherThan: ForwardApplication | |
} | |
infix operator <>: SingleTypeComposition | |
public func <> <A: AnyObject>(f: @escaping (A) -> Void, g: @escaping (A) -> Void) -> (A) -> Void { | |
return { a in | |
f(a) | |
g(a) | |
} | |
} | |
func prop<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>) | |
-> (@escaping (Value) -> Value) | |
-> (Root) | |
-> Void { | |
return { update in | |
return { root in | |
var copy = root | |
copy[keyPath: keyPath] = update(copy[keyPath: keyPath]) | |
} | |
} | |
} | |
func const<A, B>(_ x: A) -> (B) -> (A) { | |
return { _ in x } | |
} | |
public func set<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>, _ value: Value) -> (Root) -> Void { | |
return const(value) |> prop(keyPath) | |
} | |
let label = UILabel() | |
let font = UIFont.systemFont(ofSize: 12) | |
label |> set(\UILabel.font, font) <> set(\UILabel.textColor, .blue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment