I am trying to understand the true difference/usage of a pure function vs a "normal" function. Besides the argument that a pure function is particularly handy when it comes to a single usage, are there any deeper reasons on why one should go for them rather than the "normal" ones?
Asked
Active
Viewed 378 times
1
-
1I think the pros discussed on this page generally apply to the Wolfram Language as well: https://en.wikipedia.org/wiki/Anonymous_function – ktm Mar 30 '19 at 19:33
-
1See also Why are pure functions faster in Mathematica code? – Lukas Lang Mar 30 '19 at 21:07
1 Answers
1
They are inevitable to apply when you use such functions as Map or MapThread mapping binary functions on some expressions. Under binary here I mean functions taking two arguments. In this case, you want that only one position of the binary function is active.
Let me give an elementary example. Let
eq = a*x == b;
be a simple equation and assume that we want to divide both parts by a. This can be done as follows:
Map[Divide[#, a] &, eq]
(* x == b/a *)
This is the place, where the pure function is extremely useful.
Disclaimer: yes, I know, that in the latest MMa version there is a special function, DivideSides, to make such things. But this is only an example of operations of this sort. There are many others.
Alexei Boulbitch
- 39,397
- 2
- 47
- 96
-
1Suggestion for a better readability : to replace "binary function" by "two variables function". I think that a lot of people will think that binary means some kind of 0/1 or True/False function. (I'm not sure) – andre314 Mar 30 '19 at 21:06
-
@andre314: Correct as to avoiding misleading the reader. But alas, a "unary" relation (or function) takes one argument, a "binary" relation (or function) takes two arguments, a "ternary" relation", etc.... – murray Mar 30 '19 at 22:08