<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>