Last Updated: February 25, 2016
·
3.933K
· janhenrynystrom

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

1 Response
Add your response

Hah, nifty. :)

over 1 year ago ·