Giter VIP home page Giter VIP logo

pipelines's Introduction

Pipelines

Value-based processes in Swift.

// A value describing how to run a command
let ls = Command<String, [String]>(
    executable: "ls",
    serialize: {  },
    deserialize: {  }
)

// A pipeline built up of commands
let pipeline = ls                                                   // 1. Run `ls`
    .map { files -> Either<String, [String]> in                     // 2. Transform the output
        if let second = files.dropFirst().first {                   //   a. If there are 2+ files,
            return .left(url.appendingPathComponent(second).path)   //      return the second one
        } else {                                                    //   b. Otherwise,
            return .right([])                                       //      return an empty list
        }                                                           //
    }                                                               //
    .select(Pipeline(ls))                                           // 3. If we received a string, run `ls` with it

// A visual description of the pipeline
// If `Pipeline` used a Monadic `flatMap`, you wouldn't be able to tell that the 2nd
// `Command` was a call to `ls`--it'd be hidden inside the block.
//
// prints "ls » (Array<String> → Either<String, Array<String>>) » [ls]"
print(pipeline) 

// The pipeline can be run easily.
pipeline.run("path").startWithResult {  }

// Or you can inject an _executor_ to change the interpretation.
pipeline
    .run(url.path) { executable, input in
        // A mock implementation of running the `Command`.
        //
        // Print out the executable name and arguments, then read input
        // from stdin to pass on.
        print("» \(executable) \(input.arguments)")

        var lines: [String] = []
        while let line = readLine(), line != "" {
            lines.append(line)
        }
        let stdout = lines.joined(separator: "\n").data(using: .utf8)!
        let output = Pipelines.Output(exitCode: 0, stderr: Data(), stdout: stdout)
        return SignalProducer(value: output)
    }
    .startWithResult {  }

Commands can be built into Pipelines that transform the Input to the Output.

Notably, this is not done with a (Output) -> Command<Output, NewOutput> function. This means that the effects are static, so they're open to static analysis. In the above example, you could example the pipeline to test/show that it only calls ls and not rm.

Todo

  • Make more of Command static with KeyPaths
  • Document
  • Test
  • Make API public

pipelines's People

Contributors

mdiep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.