Using pattern matching one can easily create polymorphic functions in Mathematica
f[{a_, b_}] := {a^2, b^2}; f[c_Integer] := c^4; Now I want to create another function which is also polymorphic and calls the previous function, the simplest version of what I want to achieve is:
g[{a_, b_}] := f[f[{a, b}]]; g[c_Integer] := f[f[c]]; Note that the last two lines are "basically the same". What is the best way to avoid this redundancy? One possible solution I found is
Module[{a, b, c}, Scan[Apply[(g[#1] := f[f[#2]]) &],{{{a_, b_}, {a, b}},{c_Integer, c}}]] Other suggestions? Note that the "catch all" g[d_]:=f[f[d]] is "not allowed", as it will give different results for example when g[3.5] is called.
Edit: In this case the following is also possible
g[x_]:=f[f[x]] /; IntegerQ[x] || (Length[x] == 2) That might be a bit cumbersome for more complicated patterns however.
Edit 2: Also possible, same critique as above
g[x_Integer|x_List? (Length[#]==2&)]:=f[f[x]]