2

I have the following expression:

(Join[#, Reverse[#, {2}]] &@Partition[#, 2, 1, 1]) & /@ Vec

(* where: *) Vec={{2,0},{1,1},{0,2}},

but I am not sure how this expression works. I tried

Partition[#, 2, 1, 1]) & /@ Vec,{0,2}} 

and

Partition[Vec, 2, 1, 1])

but they gave me different results. I am not sure what # in the Join[] stands for (Partition[] or Vec).

In general, what is the logic behind this expression?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
lol
  • 677
  • 3
  • 7

1 Answers1

5

Due to the precedence of nested anonymous functions, the innermost expression:

Join[#, Reverse[#, {2}]] &

Is essentially identical to:

Function[{x}, Join[x, Reverse[x, {2}]]

Let's explicitly label this as function f using (yet another mostly equivalent notation):

f[x_] := Join[x, Reverse[x, {2}]]

So we can rewrite the original expression as:

(f@Partition[#, 2, 1, 1]) & /@ Vec

We can again rewrite the anonymous function, let's call it g this time:

g[x_] := f@Partition[x, 2, 1, 1]

(Or, equivalently: f[Partition[x, 2, 1, 1], rewriting the @ in perhaps more familiar terms.)

Then the expression becomes:

g /@ Vec

Which is exactly equivalent to: Map[g, Vec].

eyorble
  • 9,383
  • 1
  • 23
  • 37