Skip to content

Instantly share code, notes, and snippets.

@freddi-kit
Created October 25, 2020 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freddi-kit/112b3d13c6660339ddc85bd05ffc3c2b to your computer and use it in GitHub Desktop.
Save freddi-kit/112b3d13c6660339ddc85bd05ffc3c2b to your computer and use it in GitHub Desktop.
//
// 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