Giter VIP home page Giter VIP logo

funkit's Introduction

master develop Documentation CocoaPods

Pipelines. Promises. Railways. Your way.

FunKit is a functional toolkit for Swift. It integrates railway-oriented programming with promises, providing a novel way to build Swift applications.

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
    }

}

Pipelines

Pipelines let you build functions like shell commands, where the output of one function is the input of another. This reduces the need for temporary variables while maintaining readability, using the |> operator. The compose operator (>>>) can be used to build pipeline functions, when there isn't a value to feed into it right away.

Railways

Railways improve upon pipelines by allowing methods to return a value denoting some sort of error condition. Traditional functions can be adapted using turnout, removing the need to manually check for errors at every step.

The unwrap function converts nil values to failures, while tryCatch does the same for thrown Errors.

Promises

Promises are considered asynchronous railways. They can be created directly from railway Results, or fulfilled or rejected asynchronously. Syntactic sugar operators such as ?>, !>, and *> make it easy to integrate promises with pipelines and railways.

Lazy Evaluation

FunKit makes extensive use of @autoclosure in an effort to provide lazy evaluation. However, the performance impacts have not been looked into.

funkit's People

Contributors

jberkman avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.