2
$\begingroup$

I noticed that it is possible to construct something like f[x][y]. Displaying this in TreeForm indicates that this expression has head f[x]; and it seems to imply that this is really a function of y, and the name of this function is another function f[x]!

So what are the uses of such constructs? Could x for example be like an index that labels a family functions?

$\endgroup$
4
  • 3
    $\begingroup$ wikipedia>> Currying $\endgroup$
    – kglr
    Commented Nov 5, 2014 at 19:32
  • $\begingroup$ E.g. SortBy[Last], indexed variables x[1][t], x[2][t] in a DE system,....I just used Through[OptionValue["EventFunctions"][ics]] in this answer. $\endgroup$
    – Michael E2
    Commented Nov 5, 2014 at 19:48
  • $\begingroup$ In V10 new operator forms have been introduced. One can use SubValues to get similar functionality. It can be quite useful. $\endgroup$ Commented Nov 5, 2014 at 20:17
  • $\begingroup$ I often use it when I want to reuse expensive results. For example, in this case of function memoization, or when I want to fix some of a function's parameters and Map over another set. $\endgroup$
    – evanb
    Commented Nov 6, 2014 at 0:26

1 Answer 1

2
$\begingroup$

One way to think about this syntax is in terms of "functions with parameters that accept inputs". The normal way this is written is

function[inputs..., parameters...]

this structure leads to a somewhat awkward syntax when mapping over a list of inputs:

list = {1.234, 5.678};
(Round[#1, 0.1] & ) /@ list

(* {1.2, 5.7} *)

The two-argument-list syntax allows you to construct a parameterized operator that acts upon inputs:

round[a_][x_] := Round[x, a];
round[0.1] /@ list

(* {1.2, 5.7} *)

which is a little tidier. I believe the explosion of built-in functions which permit this syntax is due to the new Query function in version 10:

Query[round[0.1]][list]

(* {1.2, 5.7} *)

In general, there is a lot more support for more formal functional programming style in version 10.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.