I was wondering if there were any important differences or stylistic considerations for deciding between different ways to define functions of multiple variables, e.g.
f[ x_, y_ ] := x + y
f[{x_, y_}] := x + y
f[x_][y_] := x + y
Reviewing some of my code, I noticed that I do not follow any one pattern consistently. Does anyone have a principled approach?
fto have attributes likeHoldAllorListable? – Michael E2 Aug 17 '22 at 16:38f[{x_,y_}]can be handy for vector-valued functions, soNest*can be applied – Chris K Aug 17 '22 at 17:09f[{x_,y_}]is that if you are passing a small list (say a point with coordinates x,y,z). so Instead passing it as single variable sayf[pt_List]and then having the function dopt[[1]]forxandpt[[2]]foryand so on, the function can just use thex,y,znames directly. The code becomes much more clear. – Nasser Aug 17 '22 at 17:39Nest[Apply@f, {x, y},...]can be used on vector-valued outputs forf[x_, y_]. Likewise{x, y} // Apply@f. – Michael E2 Aug 17 '22 at 18:21f[x][y]evaluates to an algebraic expression, thenD[f[x][y], x]works as expected. Ifgis undefined, thenD[g[x][y], x] /. g -> fdoes not. – Michael E2 Aug 17 '22 at 18:23f[data][args], where data might be some state data, parameter values, method data, metadata, etc. Probably got this idea from exploring theNDSolveandNIntegratecaverns. – Michael E2 Aug 17 '22 at 20:04