<div dir="ltr">I'd like to have a shorthand syntax:<div><br></div><div>1> Print = fun io:format("lookee here: ~p\n")/1.</div><div>#Fun<function.0.2345234></div><div>2> Print([ stuff ]).</div><div>lookee here: stuff</div><div>ok</div><div>3></div><div><br></div><div>Print would actually be sugar for</div><div>Print = fun (X) -> io:format("lookee here: ~p\n", X) end.</div><div>with all the rules concerning funs applying in the same way.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div><br></div><div>Cheers,</div><div>-- </div><div>Pierre Fenoll</div></div><div><br></div></div></div></div>
<br><div class="gmail_quote">On 30 April 2015 at 13:42, Siraaj Khandkar <span dir="ltr"><<a href="mailto:siraaj@khandkar.net" target="_blank">siraaj@khandkar.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">One of the things I often wish for, when using Erlang, is staged application of any function without having to manually wrap it in stage funs (which you get in languages that feature curried-by-default functions).<br>
<br>
The other day it occurred to me that the ability to get fun arity info and to apply arguments as a list was all that was needed to get very, very close to the behavior I wanted. Voila:<br>
<br>
  Â  -module(function).<br>
  Â  -export([curry/1]).<br>
<br>
  Â  curry(F) -><br>
  Â  Â  Â  Info = erlang:fun_info(F),<br>
  Â  Â  Â  {value, {arity, Arity}} = lists:keysearch(arity, 1, Info),<br>
  Â  Â  Â  curry(F, [], Arity).<br>
<br>
  Â  curry(F, Args, 0) -><br>
  Â  Â  Â  apply(F, lists:reverse(Args));<br>
  Â  curry(F, Args, Arity) -><br>
  Â  Â  Â  fun (X) -> curry(F, [X | Args], Arity - 1) end.<br>
<br>
In action:<br>
<br>
  Â  $ erl<br>
  Â  1><br>
  Â  1> Nums = lists:seq(1, 10).<br>
  Â  [1,2,3,4,5,6,7,8,9,10]<br>
  Â  2><br>
  Â  2> Plus = function:curry(fun (X, Y) -> X + Y end).<br>
  Â  #Fun<function.0.4634292><br>
  Â  3><br>
  Â  3> lists:map(Plus(1), Nums).<br>
  Â  [2,3,4,5,6,7,8,9,10,11]<br>
  Â  4><br>
  Â  4> lists:map(Plus(3), Nums).<br>
  Â  [4,5,6,7,8,9,10,11,12,13]<br>
  Â  5><br>
  Â  5> lists:map(Plus(5), Nums).<br>
  Â  [6,7,8,9,10,11,12,13,14,15]<br>
  Â  6><br>
  Â  6> Fold = function:curry(fun lists:foldl/3).<br>
  Â  #Fun<function.0.4634292><br>
  Â  7><br>
  Â  7> AddTo = Fold(fun (X, Y) -> X + Y end).<br>
  Â  #Fun<function.0.4634292><br>
  Â  8><br>
  Â  8> AddTo0 = AddTo(0).<br>
  Â  #Fun<function.0.4634292><br>
  Â  9><br>
  Â  9> AddTo100 = AddTo(100).<br>
  Â  #Fun<function.0.4634292><br>
  Â  10><br>
  Â  10> AddTo0(Nums).<br>
  Â  55<br>
  Â  11> AddTo100(Nums).<br>
  Â  155<br>
  Â  12><br>
  Â  12> AddTo100(lists:seq(4, 78)).<br>
  Â  3175<br>
  Â  13><br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">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>
</blockquote></div><br></div>