With versus Function

I've seen people compare Block with Module and With as scoping constructs, and try to figure out when to choose which. Also, I've seen posts about Function versus # versus downvalue functions. However, I already understood the differences between those, but still find myself more often doubting between functions and With...

Does With have any advantage in any case over Function?

With[{sth=inj}, code code sth code]


versus

(code code # code)&[inj]


or

Function[sth, code code sth code][inj]


Apart from the fact that its syntax is better suited for defining constants since the variable name and value are "together", or other elegance issues I mean.

They are both scoping constructs, and Function among other uses can also inject code as With. If With's variables are set with := you can always use Function version with attributes.

I know, this doesn't hold in the infrequent situation when you would use a With with, e.g, 2 :=s and 2 =s which can't be emulated by a function attribute.

In the (not so extensive) tests I've done it doesn't seem to be faster.

In[127]:= TimingAverage[
With[{sum = 2 + 8},
{sum*x, sum*y}]]

Out[127]= 3.42178*10^-6

In[124]:= TimingAverage[
Function[{sum}, {sum*x, sum*y}][2 + 8]
]

Out[124]= 3.36266*10^-6

In[122]:= Module[{lalala},
lalala[sum_] := {sum*x, sum*y};
TimingAverage[
lalala[2 + 8]
]
]

Out[122]= 2.88582*10^-6

In[123]:= TimingAverage[
{#*x, #*y} &[2 + 8]
]

Out[123]= 2.67792*10^-6


Just out of curiosity, this very same test seem to be consistent with the common knowledge of Module being slower than Block being slower than With

In[119]:= TimingAverage[
Module[{sum = 2 + 8},
{sum*x, sum*y}]]

Out[119]= 7.19833*10^-6

In[120]:= TimingAverage[
Block[{sum = 2 + 8},
{sum*x, sum*y}]]

Out[120]= 4.58145*10^-6


As to the holding version of the tests, we get marginally better results with With

In[129]:= TimingAverage[
Function[{sum}, {sum*x, sum*y}, HoldFirst][2 + 8]
]

Out[129]= 4.22668*10^-6

In[128]:= TimingAverage[
With[{sum := 2 + 8},
{sum*x, sum*y}]]

Out[128]= 3.8681*10^-6

In[131]:= Module[{lalala},
SetAttributes[lalala, HoldFirst];
lalala[sum_] := {sum*x, sum*y};
TimingAverage[
lalala[2 + 8]
]
]

Out[131]= 3.45039*10^-6


EDIT

As to style and elegance, I guess that functions are more suited when the expressions you're injected are big and the code you're injecting them into is small, and With otherwise. Any thoughts on all this issue? Am I missing something?

• I know you're asking about how they behave differently, and not which one is easier to read or write, but I think the difference in readability and ease of use is very significant. Consider With[{x=1,n=2}, x^n] vs Function[{x,n}, x^n][1,2]. In Function the "constants" (i.e. x and n) are named at the beginning, at set at the end without their name being mentioned. This is a significant usability disadvantage when you just want to put inject a number of expressions into code. Otherwise I think there's no behavioural difference between them for the use you mention. Jan 26, 2012 at 9:57
• @Szabolcs Totally agree regarding the readability issue, but not quite agree rearding the differences (see my answer) Jan 26, 2012 at 13:18

To my mind, the differences are significant if obscure. The very big difference in evaluation was described already by @Sal. Here are several more subtle ones, which may however bite you. So, functions go first.

Functions

• Can be in two forms, Function[x,x^2] or Function[#^2] (the last is equivalent to #^2&), which are not always equivalent. Differences:

• Functions with named arguments are scoping constructs, and as such, perform variable collision resolution by renaming variables in inner scoping constructs, if needed. Functions with Slot-s are not quite (are in some respects but not others. Example of the difference is below), and because of that have some speed advantage.
• Functions with named arguments represent a leaky functional abstraction (see at the bottom of that answer). This matters because you can never be sure that you won't run into trouble when passing such a function as an argument. Functions with slots are ok, but can not always be nested.
• Functions with slots have a form which takes arbitrary number of arguments, such as Function[Null, Plus[##]]. Functions with named arguments don't have such form.
• Functions with slots can be made recursive, which can be a very powerful tool in some cases.
• Functions with slots, not being full - fledged scoping constructs, have the substitution semantics similar to replacement rules, in that they won't care about inner scoping constructs and possible name collisions.

Example:

With[{x=a},x+#]&[x]

(*
==> 2 a
*)


but

Function[{inj},With[{x=a},x+inj]][x]

(*
==>  a+x
*)


(we could have used Module or another Function in place of With here). Which behavior is preferred depends on the situation, but more often than not the former one is used not intentionally and leads to subtle bugs. In any case, one should be aware of this.

• As mentioned, Function - s with slots can take arbitrary number of arguments

• Functions can carry attributes. For example, this function will sort its arguments: Function[Null, {##}, Orderless]

• Because functions can carry attributes, they can hold arguments passed to them, for example: Function[expr,Head[Unevaluated[expr]],HoldAll], and also inject unevaluated arguments in their body. Functions with slots can do that for an arbitrary number of arguments as well, here is an example

• Because of their SubValue - looking form of invokation: Function[x,x^2][y], and the fact that SubValues can not be forced to hold outer groups of arguments, Function call semantics for Function-s with Hold-arguments can not be easily emulated by other means. This tells us that Function-s are very special objects, for which probably an exception was made in the main evaluation sequence semantics.

• Because Function-s can carry Hold-attributes, they can implement pass-by-reference semantics. In particular, they can change values of variables passed to them: a=1; Function[x,x=2,HoldAll][a];a.

• Because of their evaluation semantics (elaborated by @Sal), Function-s can be used to implement currying.

With

Ok, now time for With:

• With is always a scoping construct. This means it cares about inner scoping constructs and renames their variables in cases of conflicts. This is a good feature most of the time, but when it is not, there are ways to disable renaming

• With normally does evaluate the r.h.sides of its variable declarations. I recently learned (from @Szabolcs) that there is a syntax which will keep them unevaluated: With[{a := Print[1]}, Hold[a]], but it is undocumented and it is not clear if it is reliable.

• By its nature, With will always require a fixed number of arguments

• With can not normally change the values of its "variables" (constants really), unless again an undocumented form of it is used: a=1;With[{b := a}, b = 3];a.

• In principle, the core With functionality is nothing special in the sense that it can be emulated with a top-level code.

• With can be used as a r.h.s. of delayed rules. This usage allows it to share variables between its body and condition, e.g. x_:>With[{y=x^2},{x,y}/;y<10]. This is a powerful feature, and one variation of it (Trott-Strzebonski technique, a reference to the original source and some explanation can be found e.g. here) is a particularly powerful device in rule-based programming. This is a language feature, and can not be easily emulated (well, perhaps unless one uses RuleCondition) . Function-s can not be used in a similar fashion.

Some conclusions

While Function and With have somewhat similar semantics regarding the way they bind their variables, these construct are different. Even within Function-s themselves, functions with slots are substantially different from functions with named arguments, the main difference being that the former is not really a full-fledged scoping contsruct (more of a macro, as was also noted in other answers), while the latter is.

Returing to With vs Function, viewed as injecting devices - the sets of such use cases for both do have a significant overlap. In other words, in many such cases they can be used interchangeably. However, my personal inclanation is to mostly use With as an injecting device, while I use Function for such purposes in relatively special circumstances. One should also keep in mind differences outlined above, sometimes those effects can lead to subtle errors.

Finally, from the viewpoint of a programming paradigms, With plays well both with functional programming and with rules, while Function plays well with functional programming constructs, so With seems to have somewhat wider domain of applicability.

• With is just as leaky as Function. Mathematica's scoping is just not really robust. Adding a $ at the end is not proper scoping... We can rewrite @WReach's example for With: f[x_] := g[With[{a = #}, x] &]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999] Jan 26, 2012 at 13:39 • @Szabolcs, so is Module, in this case. But, Block is not, nor InternalInheritedBlock. So, this suggests that the difference in scoping types is even more significant. Jan 26, 2012 at 13:59 • @rcollyer One of the localization mechanism used by M- is adding a $ sign at the end of symbols: f[x_]:=With[{a=1},x]; TracePrint[f[1]]. This example tricks it to change a into a$ at two different places. Then the two a$s will conflict. Jan 26, 2012 at 14:05
• @Szabolcs True, but first, I did not confront With and Function in this respect (since it wasn't my intention to have a general discussion of problems of lexical scoping emulation in mma). Second, the case of With is less likely to occur in practice, due to the differences in evaluation mentioned by @Sal: Function does not evaluate immediately and this is what makes it natural to pass it as a parameter. With evaluates immediately OTOH. In your example, you had to wrap it in pure function. In its pure form, it would require functions which accept unevaluated code pieces. Jan 26, 2012 at 14:52
• @Szabolcs Sorry to disappoint... I also dislike them. All I can offer is this: gist.github.com/1683497 Jan 26, 2012 at 16:09

There is too much that can be said here so this answer is going to be incomplete but let me give it a shot.

First off, With, Block and Module are not interchangeable, in general. To understand when one is preferred you first have to understand how they are different. Performance is not the primary issue here.

With[{x=1}, ...] is appropriate when x is constant. You can't use With and update the value of x. Think of With as being like macro substitution.

Block and Module differ primarily because Block is a dynamic scoping construct and Module is a lexical scoping construct. Dynamic scoping means that all the variables defined in a Block are visible all the way down the call stack.

Example:

In[1]:= f[x_] := Block[{a = 1}, g[x]]
g[x_] := x + a

In[2]:= f[1]

Out[2]= 2


Lexically scoped Module does not work this way. Variables defined in the module are only visible to the Module.

In[3]:= f[x_] := Module[{a = 1}, g[x]]
g[x_] := x + a
f[1]

Out[3]= 1 + a


But Module and Block both allow the variables to be changed, unlike With.

In[4]:= f[x_] := Module[{a = 1}, a = a * 10; a]
f[1]

Out[4]= 10

In[5]:= f[x_] := Block[{a = 1}, a = a * 10; a]
f[1]

Out[5]= 10


Okay, now lets discuss Function versus # versus With

First thing is that Function and # are just different syntax for the same thing. These are all the same thing, just different syntax.

In[6]:= Function[# + 1][1]

Out[6]= 2

In[7]:= Function[{x}, x + 1][1]

Out[7]= 2

In[8]:= # + 1&[1]

Out[8]= 2


The difference between Function and With is very significant. With is an expression that is evaluated immediately.

y = With[{x=1}, x+1] is pretty much the same as saying y=2

but

y = Function[{x}, x+1] makes y a function that can be invoked for any value of x; y[1], y[2] and so on.

The fact that your question seems to focus on Timing these constructs indicates to me that you are largely missing the boat here. First you need to grasp the semantic differences between these constructs and when one is more appropriate than the other and then only later consider issues of performance.

• my doubt wasn't on Module and Block, I guess I wasn't clear and talked too much "introduction". And my doubt with Function and With, in your case, would be with the construction y=Function[{x},x+1][x] or #+1&[x]. I'll edit my post to make it clearer
– Rojo
Jan 26, 2012 at 4:10
• # is Slot[], not Function[]. But I got what you wanted to say. :) Jan 26, 2012 at 6:59

As I can see, the origin of your confusion is that you think that With and Function are closely related. Sal wanted to show you that they are not. Actually I will try to do the same.

The main difference is that With is a scoping construction, while Function is not primarily designed for scoping (although it can be used for this purpose). While With[{sth=inj}, code code sth code] is quite common piece of code, the analogous construction (code code # code)&[inj] is definitely not the best example of pure functions usage.

To my mind, With is most useful in writing complex symbolic expressions. For example, instead of typing

ex=(Cos[x+y] Sin[x+y]+Sin[x+y]^2)/(Cos[x+y]^3-Sin[x+y]^2)


one can put

ex=With[{s=Sin[x+y],c=Cos[x+y]},(c s+s^2)/(c^3-s^2)]


So, as Sal said, it is convenient to consider With simply as a macro substitution.

As for pure functions, most frequently they are constructed to be applied repeatedly. For example

f=(code code # code)&; Map[f, {inj1, inj2, inj3}]


Function is also convenient for quick definition of mathematical functions:

   In[16]:= f=# Sin[#]&; f'[x]+2f[y]

Out[16]= x Cos[x]+Sin[x]+2 y Sin[y]


I personally use Function much more frequently than With. Just because Function` have much more broad area of applications.