<a href="https://github.com/rabbitmq/erlando">https://github.com/rabbitmq/erlando</a> provides an implementation of monads that'll do what I think you want:<div><br></div><div><div>write_file(Path, Data, Modes) -></div>
<div>    Modes1 = [binary, write | (Modes -- [binary, write])],</div><div>    do([error_m ||</div><div>        Bin <- make_binary(Data),</div><div>        {ok, Hdl} <- file:open(Path, Modes1),</div><div>        {ok, Result} <- return(do([error_m ||</div>
<div>                                   ok <- file:write(Hdl, Bin),</div><div>                                   file:sync(Hdl)])),</div><div>        file:close(Hdl),</div><div>        Result]).</div><div><br><div class="gmail_quote">
On 24 January 2012 19:31, Reynaldo Baquerizo <span dir="ltr"><<a href="mailto:reynaldomic@gmail.com">reynaldomic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
A friend of mine asked:<br>
<br>
##<br>
If you have functions that return {ok, Result} | {error, Reason}<br>
how do you chained them? So that you have:<br>
<br>
w(x(y(z(...))))<br>
<br>
without building a staircasing. Something that would be done in Haskell<br>
with monads.<br>
##<br>
<br>
I would probably go for:<br>
<br>
x({ok, Value}) -><br>
  NewValue = <do something with Value>,<br>
  {ok, NewValue};<br>
x({error, Reason}) -><br>
  {error, Reason}.<br>
<br>
in each function<br>
<br>
which brings me other question, when do you tag return values?<br>
<br>
I tend to only use tagged return values with impure functions, were an<br>
error is more likely due to side effects.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Reynaldo<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</font></span></blockquote></div><br></div></div>