Created
October 25, 2020 01:24
-
-
Save freddi-kit/112b3d13c6660339ddc85bd05ffc3c2b 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
// | |
// ViewController.swift | |
// FunctionalComposedControllerSample | |
// | |
// Created by freddi on 2020/10/16. | |
// | |
import UIKit | |
infix operator |> | |
func |> <A: AnyObject>(v: A, f: @escaping (A) -> Void) -> A { | |
f(v) | |
return v | |
} | |
precedencegroup ApplyOperator { | |
associativity: left | |
} | |
public func |> <A, B>(_ lhs: A, _ rhs: @escaping (A) -> B) -> B { | |
return rhs(lhs) | |
} | |
infix operator <> | |
func <> <A: AnyObject>(f: @escaping (A) -> Void, g: @escaping ( A) -> Void) -> ( A) -> Void { | |
return { a in | |
f(a) | |
g(a) | |
} | |
} | |
func modifyColor(color: UIColor) -> (UIButton) -> () { | |
return { button in | |
button.backgroundColor = color | |
} | |
} | |
func modifyAsAccountInfoButton(button: UIButton) { | |
button.layer.cornerRadius = 20 | |
button.clipsToBounds = true | |
button.setTitleColor(.white, for: .normal) | |
} | |
let normalLoginStyle = modifyColor(color: .green) <> modifyAsAccountInfoButton | |
let logoutStyle = modifyColor(color: .red) <> modifyAsAccountInfoButton | |
let twitterStyle = modifyColor(color: .blue) <> modifyAsAccountInfoButton | |
class ViewController: UIViewController { | |
let loginButton = UIButton() |> (normalLoginStyle) | |
let logoutButton = UIButton() |> (logoutStyle) | |
let loginWithTwitterButton = UIButton() |> (twitterStyle) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Add button's to view ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment