2
$\begingroup$

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?

$\endgroup$
1
5
$\begingroup$

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].

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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