FunKit 0.1.0

FunKit 0.1.0

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最后发布2017年12月
SwiftSwift 版本3.2
SPM支持 SPM

jacob berkman 维护。



FunKit 0.1.0

  • jacob berkman

master
develop
Documentation
CocoaPods

管道。承诺。铁路。您的选择。

FunKit 是一个为 Swift 量身定制的函数式工具包。它将针对 铁路式编程承诺 的集成,提供构建 Swift 应用的创新方法。

enum App {

    static func main(context: AppContext) -> Bool {

        func addOKAction(alert: UIAlertController) {
            (title: "OK", style: .default, handler: nil)
                |> UIAlertAction.init
                |> alert.addAction
        }

        let showAlert = UIAlertController.init(title:message:preferredStyle:)
            >>> tee(addOKAction)
            >>> curry(reverse(context.viewController.present))(nil)(true)

        func success<A>(_: A) {
            (title: "Initialized", message: nil, preferredStyle: .alert)
                |> showAlert
        }

        func failure(error: Error) {
            (title: "Failed", message: error.localizedDescription, preferredStyle: .alert)
                |> showAlert
        }

        func done() { print("Initialization complete") }
        
        func returnTrue<A>(_: A) -> Bool { return true }

        return ()
            |> context.initialize
            |> Promise.main
            ?> success
            !> failure
            *> done
            |> returnTrue
    }

}

管道

管道允许您构建像 shell 命令那样的函数,其中一个函数的输出来作为另一个函数的输入。这不仅减少了临时变量的需要,还保持了可读性,使用了 |> 操作符。当没有即时的值可以输入时,可以使用组合操作符(>>>)来构建管道函数。

铁路

铁路 通过允许方法返回表示某种错误条件的值来改进管道。可以使用 turnout 对传统函数进行适配,从而摆脱了在每个步骤上手动检查错误的需要。

unwrap 函数将 nil 值转换为失败,而 tryCatch 则对抛出的 Error 执行相同的操作。

承诺

承诺 被视为异步铁路。它们可以直接从铁路 Result 中创建,或异步地实现或拒绝。如 ?>!>*> 等语法糖操作符使承诺与管道和铁路的集成变得容易。

惰性求值

FunKit 在提供惰性求值方面广泛使用了 @autoclosure,但尚未对性能影响进行研究。