Skip to content

Instantly share code, notes, and snippets.

@klaaspieter
Created March 14, 2018 14:49
Show Gist options
  • Save klaaspieter/f3a18b5e32cddbd083c403b7fdba355e to your computer and use it in GitHub Desktop.
Save klaaspieter/f3a18b5e32cddbd083c403b7fdba355e to your computer and use it in GitHub Desktop.
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