Giter Club home page Giter Club logo

pipes's Introduction

Pipes

Carthage compatible MIT License


This is a Swift framework that provides forward and back pipe operators.

The forward pipe operator allows you to apply the left hand side of the expression as the first argument in the function on the right.

func increment(x: Int) -> Int {
    return x + 1
}

2 |> increment // returns 3

Using the . operator is the most common and clean way in most cases to chain together instance method calls.

let isEven: Int->Bool = { x in x % 2 == 0 }
[1,2,3,4,5].filter(isEven).map({$0 * 3}).reduce(0, combine: +)

However, the above example does not generalize to sequences, as the functions to operate on sequences are not members of Sequence. When working on Sequences, the code would look like:

reduce(map(filter([1,2,3,4,5], isEven), {$0 * 3}), 0, +)

This code is harder to read and reason about. The forward pipe operator offers a solution:

[1,2,3,4,5]
   |> (filter, isEven)
   |> (map, {$0 * 3})
   |> (reduce, 0, +)

Using the forward pipe operator, the code reads as a list of functions in a processing pipeline. It also helps avoid the introduction of temporary variables which only exist to store an argument into a function that will be called on the next line.

Optionals

Futhermore, the pipeline operator even allows you to work simply with Optionals and Results!

// searches elements for 6, and then checks to see if the index is even
let foo = [2, 4, 6, 8, 10]
    |> (find, 6)
    |> isEven

// without the pipe operator
let foo: Bool?
if let x = find([2, 4, 6, 8, 10], 6) {
    foo = isEven(x)
}
else {
    foo = nil
}

In this example, we are able to use the find() function, which returns an Optional, and immediately pipe it into isEven, which does not take an optional. If one of the functions in the chain ever returns nil, the whole thing will short circuit and the result of the expression will be nil.

Results

The forward pipe also works seamlessly with Results! The expression will short circuit as soon as something returns a Failure case. Otherwise, it will continue to apply the associated value of the Success into the next function.

func escapeInput(string: String) -> String { ... }

func readFile(fileName: String) -> Result<String> { ... }

func processText(string: String) -> String { ... }

inputFileName
    |> escapeInput
    |> readFile
    |> processText

The above code shows function stubs and how one might chain them together in order to take some text input, read the corresponding file, and return some processed text. Of course, reading a file could potentially fail if the file can not be found, or permissions do not allow it. So, the readFile method could potenentially return a Failure result. If so, the entire expression will then short circuit and evaluate to the Failure. Otherwise it will continue processing. Regardless, the final result will be of type Result<String>, because the return type of processText is String. If we were to add another pipe, say into |> count, then the final result of the expression would be of type Result<Int>.

Variations

The standard forward pipe operator applies the left hand side as the first argument in the function on the right hand side. However, there may be cases in which you wish to apply the left hand side in a different position. Algebraic supplies 3 helper pipe operators

  • |>> applies the left hand side as the second argument
  • |>>> applies the left hand side as the third argument
  • |< applies the left hand side as the last argument

Resources

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.