Smajl
Smajl

Reputation: 7995

Mathematica syntax with & symbol

I have come across this expression in Mathematica:

oneStep[plus[e1_ , e2_]] := Flatten[{With[{a=e1,b=#},plus[a,b]]&/@oneStep[e2],
                                  With[{a=#,b=e2},plus[a,b]]&/@oneStep[e1]}];

but I cant seem to understand what does this &/ symbol mean in this expression.

Secondly: can this be written in more "human-friendly" way?

Upvotes: 1

Views: 1271

Answers (2)

cormullion
cormullion

Reputation: 1672

To supplement arshajii's answer:

veryLongFunctionName[n_] := n + n/2;
Map[veryLongFunctionName, {1, 2, 3}]

which returns:

{3/2,3,9/2}

is longer than:

Map[# + #/2 &, {1, 2, 3}]

which is longer than:

# + #/2 & /@ {1, 2, 3}

Upvotes: 2

arshajii
arshajii

Reputation: 129517

The & signifies a pure function (which is kind of like a lambda). Yes, it can be written in a friendlier way. As the linked documentation indicates:

body&

is equivalent to

Function[x,body]

where x is the argument.

The /@ is a map (which can also be written in a friendlier way, as you can see from the docs).

Upvotes: 3

Related Questions