# Is there a bug in AssociatePairs?

Bug introduced in 10.0 and fixed in 10.1.0

In Version 10, the GeneralUtilities package contain some useful functions. AssociatePairs is one of those functions included in the package and advertised to work as follows:

Needs["GeneralUtilities"]

?AssociatePairs


Well, here is an input that matches a List of pairs:

lis = Table[{k, k^3}, {k, 2, 10}];


Now,

AssociatePairs[lis]


Gives the following error:

Well, the included PairQ function disagrees with this assertion:

AllTrue[lis, PairQ]


True

So, is this a bug? Since I can't get any list of pairs to work with this function. I'm on Windows 8.1

• Looks like a bug for me, why not to report to Wolfram Support? – m0nhawk Jul 16 '14 at 6:14
• @m0nhawk, I need more people to confirm this behavior and on different operating systems. – RunnyKine Jul 16 '14 at 6:18
• confirm on Windows 8.1 (x64), will check on Linux in few hours. – m0nhawk Jul 16 '14 at 6:20
• is GeneralUtilities supposed to be an official thing? or some kind of still-in-work package? If it is official, where to find official documentation about it? When I type reference.wolfram.com/search/?q=GeneralUtilities I do not see it? – Nasser Jul 16 '14 at 6:39
• Error persists in 10.0.1. – Sjoerd C. de Vries Sep 17 '14 at 14:29

Using Simon Woods's Spelunk we see that the body of the function is:

Self : AssociatePairs[l_] :=
Module[{r$}, r$ = Which[! Quiet[TrueQ[MatrixQ[l, PairQ]]], Message[AssociatePairs::npairs, l];
$FailRHS, True, HoldSequence[Associate @@ Transpose[l]]]; ReleaseHoldSequence[r$] /; ! MatchQ[r$,$FailRHS | HoldSequence[\$FailRHS]]
]


TrueQ[MatrixQ[l, PairQ]]


Recalling the definition of MatrixQ:

This means that the function is checking that:

1. l (input) is a matrix

2. each element passes PairQ

This is apparently illogical.

One could correct this with one of many other possible checks, simplest perhaps:

MatchQ[l, {{_, _} ..}]


Or to allow for SparseArray input, as may have been the motivation for MatrixQ:

MatchQ[Dimensions[l], {_, 2}] && MatrixQ[l]

• That is an interesting condition! As an aside, every time I use spelunk I want to hug @SimonWoods – acl Jul 16 '14 at 11:02
• Yeah, the condition is clearly off. Hopefully it will be corrected when they finally add it as an internal function. – RunnyKine Jul 16 '14 at 11:36
• This is fixed, which is too late for 10.0.2, but will be present in 10.0.3 – Taliesin Beynon Dec 11 '14 at 16:01
• @Taliesin Thanks for the notice. – Mr.Wizard Dec 11 '14 at 19:24
• Didn't we go from 10.0.2 to 10.1? – Sjoerd C. de Vries Jul 25 '15 at 22:28

This is an adjunct to @Mr.Wizard's answer: while I've fixed this for 10.0.3, anyone who wants to use this rather trivial function in the meantime can run SystemOpen["GeneralUtilitiesLoader"] and then paste the following code at the end of that file:

Begin["GeneralUtilitiesGeneralPackagePrivate"]
AssociatePairs[l_] := MacrosConditionalRHS[
(MatrixQ[l] && Dimensions[l][[2]] == 2) || VectorQ[l, PairQ], {"npairs", l},

P.S. What goodies does Macros contain? Only the intrepid spelunker will find out!