[erlang-questions] chained functions

Gianfranco Alongi gianfranco.alongi@REDACTED
Tue Jan 24 22:17:47 CET 2012


If all your functions F_1 ... F_K have the type

F({ok,Result} | {error,Reason} ) ->  {ok,Result} | {error,Reason}

then I would probably write a small sequencer as

seq({ok,Result},[ F | T ]) ->
  seq(F(Res),T);
seq(Error,T) ->
  Error.

And call it like

seq({ok,Initial},[ fun F_1, ..., fun F_K ])

I always try to tag values, so you can use it to determine flow control.

Cheers
G

On Tue, Jan 24, 2012 at 8:31 PM, Reynaldo Baquerizo
<reynaldomic@REDACTED> wrote:
>
> A friend of mine asked:
>
> ##
> If you have functions that return {ok, Result} | {error, Reason}
> how do you chained them? So that you have:
>
> w(x(y(z(...))))
>
> without building a staircasing. Something that would be done in Haskell
> with monads.
> ##
>
> I would probably go for:
>
> x({ok, Value}) ->
>  NewValue = <do something with Value>,
>  {ok, NewValue};
> x({error, Reason}) ->
>  {error, Reason}.
>
> in each function
>
> which brings me other question, when do you tag return values?
>
> I tend to only use tagged return values with impure functions, were an
> error is more likely due to side effects.
>
> --
> Reynaldo
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list