1

The following code is taken from Wolfram Community "Quantum Error Correction: bit flip".

GroupBy[{#,BitXor@@@Partition[#,2,1]}&/@Tuples[{0,1},3],Last->First] 
KeyValueMap[<|"Syndrome" -> #1, "States/Bits" -> ToString@#2|> &][%] // Dataset

My question is very simple.

In the second line of code, why is it necessary to have the "&" inside the KeyValueMap function? Why is it not KeyValueMap[<|...|>]&[%]//Dataset ?

I know that the above is wrong, I am just not sure why?

In the Wolfram Language reference page, the explain slot as

f[#1, #2, #1, #3] &[x, y, z]

Here, the "&" is outside the dunction?

am567
  • 501
  • 8
  • 2
    You are looking at the operator form of KeyValueMap. An alternative syntax would be KeyValueMap[<|"Syndrome" -> #1, "States/Bits" -> ToString@#2|> &, %] // Dataset. – Syed Aug 11 '23 at 11:06
  • I see, that form is more familiar to me. Is it possible to explain why it works when it is inside the operator KeyValueMap? Also, what is the purpose of the comma in the alternative syntax you provided? thanks! – am567 Aug 11 '23 at 11:31
  • 1
    From the Details section on the doc page: KeyValueMap[f][assoc] is equivalent to KeyValueMap[f,assoc]. The definition of a pure function must end with an & sign. – Syed Aug 11 '23 at 13:22
  • 2
    As an interesting aside that you may already be aware of: f[#1, #2, #1, #3] &[x, y, z, x1,y1,z1,...] gives the same result as f[#1, #2, #1, #3] &[x, y, z], as will g[x, y, z, x2, y2, z2,...] where g=f[#1, #2, #1, #3] &;. This is because there is no pattern matching on a 'pure' function, and is well explained in Wagner p 195. Contrast with the behaviour of, say, gg[a_,b_,c_]:=f[a,b,a,c] (where there is pattern matching) – user1066 Aug 11 '23 at 19:13

0 Answers0