All Projects → aslrousta → pipe

aslrousta / pipe

Licence: MIT license
Functional Pipeline in Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to pipe

Pipeline
Pipeline is a package to build multi-staged concurrent workflows with a centralized logging output.
Stars: ✭ 433 (+1343.33%)
Mutual labels:  pipeline, pipe
Onhold
🔊 Play sounds while and after shell jobs complete
Stars: ✭ 146 (+386.67%)
Mutual labels:  pipeline, pipe
Ttyplot
a realtime plotting utility for terminal/console with data input from stdin
Stars: ✭ 532 (+1673.33%)
Mutual labels:  pipeline, pipe
tpack
Pack a Go workflow/function as a Unix-style pipeline command
Stars: ✭ 55 (+83.33%)
Mutual labels:  pipeline, pipe
Syntax sugar python
A library adding some anti-Pythonic syntatic sugar to Python
Stars: ✭ 721 (+2303.33%)
Mutual labels:  functional, pipeline
pypely
Make your data processing easy
Stars: ✭ 17 (-43.33%)
Mutual labels:  pipeline, pipe
Hookah
A cross-platform tool for data pipelines.
Stars: ✭ 83 (+176.67%)
Mutual labels:  pipeline, pipe
Param pipe
parameterized pipe in elixir: |n>
Stars: ✭ 14 (-53.33%)
Mutual labels:  pipeline, pipe
pipe
Pipe operator for nim.
Stars: ✭ 51 (+70%)
Mutual labels:  functional, pipe
Pipeline.rs
☔️ => ⛅️ => ☀️
Stars: ✭ 188 (+526.67%)
Mutual labels:  pipeline, pipe
rocket-pipes
Powerful pipes for TypeScript, that chain Promise and ADT for you 🚌 -> ⛰️ -> 🚠 -> 🏂 -> 🚀
Stars: ✭ 18 (-40%)
Mutual labels:  pipeline, pipe
Pipetools
Functional plumbing for Python
Stars: ✭ 143 (+376.67%)
Mutual labels:  functional, pipe
etran
Erlang Parse Transforms Including Fold (MapReduce) comprehension, Elixir-like Pipeline, and default function arguments
Stars: ✭ 19 (-36.67%)
Mutual labels:  pipeline, pipe
pipe-trait
Make it possible to chain regular functions
Stars: ✭ 22 (-26.67%)
Mutual labels:  pipeline, pipe
Fluids
Fluid dynamics component of Chemical Engineering Design Library (ChEDL)
Stars: ✭ 154 (+413.33%)
Mutual labels:  pipeline, pipe
Pipe
[READONLY] Library for implementing function call chains
Stars: ✭ 70 (+133.33%)
Mutual labels:  functional, pipe
Rangeless
c++ LINQ -like library of higher-order functions for data manipulation
Stars: ✭ 148 (+393.33%)
Mutual labels:  functional, pipeline
Aioreactive
Async/await reactive tools for Python 3.9+
Stars: ✭ 215 (+616.67%)
Mutual labels:  functional
frizzle
The magic message bus
Stars: ✭ 14 (-53.33%)
Mutual labels:  pipeline
Falco
A functional-first toolkit for building brilliant ASP.NET Core applications using F#.
Stars: ✭ 214 (+613.33%)
Mutual labels:  functional

Go Pipe

GitHub GoDoc

Go Pipe is a simple implementation of pipe operator in Go.

What is it all about?

One of the most intereseting features of functional languages is the ability to easily compose functions. The function composition is the operation that takes two functions and produces another function in the way that the result of the second function is passed as the input argument of first one.

Function composition is so common in functional programming which most of the functional languages have defined a special operator, typically called a pipe operator, to make composition even simpler. For example, in Haskell we have the . (dot) operator, and Elixir defines the |> (pipe) operator:

append = fn list, item ->
  list
  |> Enum.reverse
  |> prepend.(item)
  |> Enum.reverse
end

Actually, in any programming language which supports functional style we can achieve the same result by applying functions consequently. For example in Go, we can write:

func h(x int) int {
    f := func(x int) int {
        if x < 0 {
            return -x
        } else {
            return x
        }
    }

    g := func(x int) int {
        return x * x
    }

    return f(g(x))
}

But the problem arises when one of the func's returns multiple values, as is common with returning errors. In that case we need to handle error specifically and write:

func h(x int) (int, error) {
    f := func(x int) (int, error) {
        if x < 0 {
            return 0, errors.New("x should not be nagtive")
        }
        return x, nil
    }

    g := func(x int) int {
        return x * x
    }

    y, err := f(x)
    if err != nil {
        return 0, err
    }

    return g(y), nil
}

This is not much complex for two func's, but it can easily become a burden if the number of func's grows.

To overcome this complexity I've written this library to make life simpler for those who love functional style like me. Go Pipe provides a Pipe func which composes one or more funcs into a pipeline func, which can be invoked anytime withing the code.

Using Pipe func is very simple:

import . "github.com/aslrousta/pipe"

func h(x int) int {
    var result int

    pipe := Pipe(
        func(x int) int {
            if x < 0 {
                return -x
            } else {
                return x
            }
        },
        func(x int) {
            result = x * x
        },
    )

    pipe(x)
    return result
}

And, it can handle errors automatically:

import . "github.com/aslrousta/pipe"

func h(x int) (int, error) {
    var result int

    pipe := Pipe(
        func(x int) (int, error) {
            if x < 0 {
                return 0, errors.New("x should not be nagtive")
            }
            return x, nil
        },
        func(x int) {
            result = x * x
        },
    )

    err := pipe(x)
    return result, err
}

License

Go Pipe source is released to the public under MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].