Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// 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