Erlang: Poor mans partial application
-module(partial).
-export([apply/4]).
apply(Module, Name, Arity, Args) when length(Args) < Arity ->
Left = Arity - length(Args),
fun(Args1) when length(Args1) < Left ->
fun(Args2) ->
apply(Module, Name, Arity, Args2 ++ Args1 ++ Args)
end;
(Args1) when length(Args1) > Left ->
erlang:error(badarg);
(Args1) ->
erlang:apply(Module, Name, Args1 ++ Args)
end;
apply(_, _, Arity, Args) when length(Args) > Arity ->
erlang:error(badarg);
apply(Module, Name, _, Args) ->
erlang:apply(Module, Name, Args).
Written by Jan Henry Nystrom
Related protips
1 Response
Hah, nifty. :)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Erlang
Authors
Related Tags
#erlang
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#