[erlang-questions] Must and May convention

Fred Hebert <>
Fri Sep 29 16:05:12 CEST 2017


On 09/29, zxq9 wrote:
>Why would this not be a pipeline function in the lists module?
>
>It is actually done that way in more than a few inner project libs I've
>worked with.
>
>As an aside, I've always wondered why the lists module doesn't have
>any pipelines, considering how often this sort of discussion comes up
>and how quickly and cleanly it is solved inside various projects with
>a pipeline function (or set of them that have different behaviors).
>

It could be. That's how the fancier forms are implemented in most cases 
I've seen. The fancyflow forms I've mentioned earlier in the thread are 
in fact just macros over to run:

    fancyflow:pipe(InitialState, [fun(Var) -> Exp1 end,
                                  fun(Var) -> Exp2 end,
                                  ...,
                                  fun(Var) -> ExpN end])
    fancyflow:maybe(InitialState, [fun(Var) -> Exp1 end,
                                   fun(Var) -> Exp2 end,
                                   ...,
                                   fun(Var) -> ExpN end])

Where there's nothing quite magical about them. At that point, they're 
just a bit verbose and cumbersome to write. You're probably better off 
not using the pipe function and just use variables like `X1 = Exp1, X2 = 
Exp2, ..., XN = ExpN', and it will be shorter to read at a minimal 
maintenance cost. The one place it gets to be nicer is when the 
operations to be run are higher order functions to begin with, and not 
just a local abstraction over literal code that would not require funs 
otherwise.

The `maybe' function would probably still be worth it though. Nested 
cases are annoying enough to be more costly to maintain than the 
abstracted version.

I'm not quite sure 'lists' would be the module for them; they're kind of 
flipping the concept of a 'fold' where you apply a function to multiple 
list elements updating a single state to 'applying a list of functions 
to a term'. They're definitely mappable to the lists:foldl operations, 
but it feels like the 'lists' part of it is not really what matters 
here, but the control flow is.




More information about the erlang-questions mailing list