X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fhope_fun.erl;h=96a39cda7fdf7be7f8f79d2ae8d3bcaac859d77a;hb=0b6ed6177fd85b5dd1b6ceadb9c2e2560c6597d2;hp=1f8669cfe536903c140fd618233662bc94a2e7e0;hpb=87160c0adda38ad3c307cc4072a03ab606f359ef;p=hope.git diff --git a/src/hope_fun.erl b/src/hope_fun.erl index 1f8669c..96a39cd 100644 --- a/src/hope_fun.erl +++ b/src/hope_fun.erl @@ -3,6 +3,9 @@ -export( [ id/1 , curry/1 + , compose/1 % alias for compose_right/1 + , compose_right/1 + , compose_left/1 ]). -spec id(A) -> @@ -13,7 +16,7 @@ id(X) -> -spec curry(fun()) -> fun(). curry(F) -> - {some, Arity} = hope_kv_list:get(erlang:fun_info(F), arity), + {arity, Arity} = erlang:fun_info(F, arity), curry(F, [], Arity). -spec curry(fun(), list(), integer()) -> @@ -22,3 +25,26 @@ curry(F, Args, 0) -> apply(F, lists:reverse(Args)); curry(F, Args, Arity) -> fun (X) -> curry(F, [X | Args], Arity - 1) end. + +-spec compose([fun((A) -> B)]) -> + fun((A) -> B). +compose(Fs) -> + compose_right(Fs). + +-spec compose_right([fun((A) -> B)]) -> + fun((A) -> B). +compose_right(Fs) -> + compose_given_fold(Fs, fun lists:foldr/3). + +-spec compose_left([fun((A) -> B)]) -> + fun((A) -> B). +compose_left(Fs) -> + compose_given_fold(Fs, fun lists:foldl/3). + + +%% ============================================================================ + +-spec compose_given_fold([fun((A) -> B)], fun((fun((A, B) -> C), B, [A]) -> C)) -> + fun((A) -> C). +compose_given_fold(Fs, Fold) -> + fun (X) -> Fold(fun (F, Y) -> F(Y) end, X, Fs) end.