[erlang-questions] Pipe Operator in Erlang?

Pierre Fenoll pierrefenoll@REDACTED
Fri Jul 10 07:17:07 CEST 2015


A use case I often bump into is modifying some state using the accessors
already written for this structure.

For example, modifying a record:

    PvtFuns = [ fun add_pvt_type/1
                     , fun add_pvt_vsn/1
                     , fun maybe_add_pvt_api_key/1
                     , fun maybe_add_pvt_tree/1
                     , fun add_pvt_enabled/1
    NewRecord = lists:foldl(fun(F, C) -> F(C) end, Record, PvtFuns),

Or some opaque structure, with accessors written more seriously:

    NewContext =
        cb_context:setters(Context, [ {fun cb_context:set_doc/2, []}
                                                     , {fun
cb_context:set_resp_status/2, 'success'}
                                                     , {fun
cb_context:set_resp_data/2, []}
                                                     , {fun
cb_context:set_resp_etag/2, 'undefined'}

Where setters/2 is a call to lists:foldl/3.

A pipe operator would turn this structure-specific code into something more
readable and editable too.
Maybe like something like this:

    NewContext = Context
        |> cb_context:set_doc(_, [])
        |> cb_context:set_resp_status(_, success)
        |> cb_context:set_resp_data(_, [])
        |> cb_context:set_resp_etag(_, undefined)

Where m:f(_, A2) is fun (_1) -> m:f(_1, A2) end; thus redefining _ for
pipes (in non-matching contexts…).

Scoping rules for nested pipes looks like a hard problem, but I am not sure
nestedness would be a great thing to have…

Pierre Fenoll

On 9 July 2015 at 19:01, Richard A. O'Keefe <ok@REDACTED> wrote:

> For what it's worth, I implemented |> in my Smalltalk system.
> It's astonishing how useful it isn't.
> I use o in ML and I use . in Haskell but in Smalltalk it
> just. does. not. fit.  In the relevant respects (no built-in
> currying, distinction between named and anonymous functions)
> Erlang and Smalltalk are so similar that I'd expect |> to be
> just as useless in Erlang as I found it in Smalltalk.
> (When I say |> was useless, I'm being euphemistic.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150709/00173b8b/attachment.htm>

More information about the erlang-questions mailing list