-export(
[ id/1
+ , curry/1
]).
-spec id(A) ->
A.
id(X) ->
X.
+
+-spec curry(fun()) ->
+ fun().
+curry(F) ->
+ {value, {arity, Arity}} = lists:keysearch(arity, 1, erlang:fun_info(F)),
+ curry(F, [], Arity).
+
+-spec curry(fun(), list(), integer()) ->
+ fun().
+curry(F, Args, 0) ->
+ apply(F, lists:reverse(Args));
+curry(F, Args, Arity) ->
+ fun (X) -> curry(F, [X | Args], Arity - 1) end.
%% Test cases
-export(
[ t_id/1
+ , t_curry/1
]).
groups() ->
Tests =
[ t_id
+ , t_curry
],
Properties = [parallel],
[ {?GROUP, Properties, Tests}
t_id(_Cfg) ->
X = foo,
X = hope_fun:id(X).
+
+t_curry(_Cfg) ->
+ Single = fun (X) -> X end,
+ Double = fun (X, Y) -> {X, Y} end,
+ Triple = fun (X, Y, Z) -> {X, Y, Z} end,
+
+ F = hope_fun:curry(Single),
+ a = F(a),
+
+ G1 = hope_fun:curry(Double),
+ G = G1(a),
+ {a, b} = G(b),
+
+ H1 = hope_fun:curry(Triple),
+ H2 = H1(a),
+ H = H2(b),
+ {a, b, c} = H(c).