Completely new to Erlang. I'm trying to define some functions for function composition, such as compose
, juxt
and pipe
but running into the fact that Erlang doesn't have (to my knowledge) varargs so it's difficult to write just one version of such functions that will work for all inputs.
So far my best idea is to hardcode functions of different arities up to a reasonable number, as well as providing a version that takes a list for anything bigger, like this:
pipe (X, Fs) when is_list(Fs) -> lists:foldl(fun (F, Acc) -> F(Acc) end, X, Fs);
pipe (X, F) -> F(X).
pipe (X, F, G) -> G(F(X)).
pipe (X, F, G, H) -> H(G(F(X))).
pipe (X, F, G, H, I) -> I(H(G(F(X)))).
pipe (X, F, G, H, I, J) -> J(I(H(G(F(X))))).
pipe (X, F, G, H, I, J, K) -> K(J(I(H(G(F(X)))))).
pipe (X, F, G, H, I, J, K, L) -> L(K(J(I(H(G(F(X))))))).
which works, but I'm curious if there's a better way?