Giter Club home page Giter Club logo

epipe's Introduction

epipe

Build Status Hex pm hex.pm downloads

Erlang pipes

Influenced by Elixir pipe (|>) and with operators. Brings similar functionality to erlang

Intro

Probably you've seen code like this:

case gen_tcp:connect(Host, Port, Otps) of
    {ok, Socket} -> 
        case do_handshake(Socket) of
            ok -> send_message(Socket);
            {error, Reason} -> exit(normal)
        end;
    {error, Reason} -> exit(normal)
end

Sometimes the amount of the nested cases goes even deeper.... Which makes the code quite complex for understanding and debugging.

Epipe allows to rewrite it in a very flat way:

connect({Host, Port, Opts}) -> 
    case gen_tcp:connect(Host, Port, Otps) of
        {ok, Socket} -> {ok, Socket};
        {error, Reason} -> {error, Reason}
    end.

handshake(Socket) -> 
    case do_handshake(Socket) of
        ok -> {ok, Socket};
        {error, Reason} -> {error, Reason}
    end.

send_message(Socket) ->
    case do_send_message(Socket) of
        response -> {ok, response};
        Error -> {error, Error}
    end.

FunctionsList = [
    {connect_fun, fun connect/1}, 
    {handshake_fun, fun handshake/1}, 
    {send_message_fun, fun send_message/1}
],
epipe:run(FunctionsList, {Host, Port, Opts}).

Which allows to 'pipe' the initial data through FunctionsList, stopping on any step which would produce {error, ...} value.

Examples

sample1() -> 
    Fun1 = fun(Val) -> {ok, Val + 1} end,
    Fun2 = fun(Val) -> {ok, Val + 2} end,
    epipe:run([{add_one, Fun1}, {add_two, Fun2}], 0).

Would produce {ok, 3}

sample2() -> 
    Fun1 = fun(Val) -> {ok, Val + 1} end,
    Fun2 = fun(Val) -> {error, "Can't process data"} end,
    epipe:run([{step1, Fun1}, {step2, Fun2}, {step3, Fun1}], 0).

Would produce {error, step2, "Can't process data", 1}, giving not only error reason, but also would give a hint, about the failing step.

Build

$ rebar3 compile

epipe's People

Contributors

oltarasenko avatar raydrawc avatar

Stargazers

Geeny avatar  avatar  avatar

Watchers

James Cloos 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.