Skip to content

patgoley/Pipeline

Repository files navigation

Pipeline

Build Status CocoaPods Carthage compatible Hex.pm

Overview

Pipeline is a framework that empowers functional programming in Swift. It chains together functions and objects into "pipelines" that produce, transform, or consume data in a linear fashion.

Take the following components that make up a very basic networking library:

func requestforURL(url: NSURL) -> NSURLRequest {
    ...
}

class HTTPClient: TransformerType {

    typealias InputType = NSURLRequest

    typealias OutputType = NSHTTPURLResponse

    ...
}

func dataFromResponse(response: NSHTTPURLResponse) -> NSData {
    ...
}

func JSONObjectFromData(data: NSData) -> [String: AnyObject] {
    ...
}

You can probably imagine the implementations of these components without much trouble, each one is meant to be simple and only do one thing. So how can we string these together with Pipeline? Like so!

let getObjectPipeline = requestforURL |> HTTPClient() |> dataFromResponse |> JSONObjectFromData

//getObjectPipeline is TransformerPipeline<NSURL, [String, AnyObject]>

So after chaining these components together, we end up with a TransformerPipeline. This pipeline is nothing special really, just TransformerType that takes in an NSURL and ultimately produces a [String: AnyObject], using the functions and object we strung together to create it. Let's use this pipeline to get some JSON from Reddit.

let url = NSURL(string: "https://www.reddit.com/r/science.json")

getObjectPipeline.consume(url) { json in

    print("science subreddit object: \(json)")
}

What's great about the pipeline just being a TransformerType is that we can chain it up to any function, object, or pipeline that produces an NSURL or that consumes a [String: AnyObject]. This allows us to make things that are reusable based on their input or output type, instead of having some unique interface that must be glued together with other interfaces using extra code.

Installation

pod 'Pipeline', '~> 2.0'

License

Pipeline is available under the Apache 2 License. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages