[erlang-questions] chained functions
Tim Watson
watson.timothy@REDACTED
Tue Jan 24 21:22:32 CET 2012
https://github.com/rabbitmq/erlando provides an implementation of monads
that'll do what I think you want:
write_file(Path, Data, Modes) ->
Modes1 = [binary, write | (Modes -- [binary, write])],
do([error_m ||
Bin <- make_binary(Data),
{ok, Hdl} <- file:open(Path, Modes1),
{ok, Result} <- return(do([error_m ||
ok <- file:write(Hdl, Bin),
file:sync(Hdl)])),
file:close(Hdl),
Result]).
On 24 January 2012 19:31, 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120124/4069711d/attachment.htm>
More information about the erlang-questions
mailing list