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