12
$\begingroup$

Bug fixed in 10.0.0


Today I lost a lot of time with a strange behavior and I narrowed it down to this small piece of code:

a = {1, 2};
ToString@Row[DeleteDuplicates[a], ","]
ToString@Row[Union[a], ","]
"Row[{1, 2}, ,]"
"1,2"

Can someone explain this bizarre behavior? Is this a bug?

This is interesting too:

a = {1, 2};
l1 = DeleteDuplicates[a];
l2 = Union[a];
l1==l2
s1 = ToString@Row[l1, ","]
s2 = ToString@Row[l2, ","]
s1==s2
True
"Row[{1, 2}, ,]"
"1,2"
False

There is some "memory" in l1 created using DeleteDuplicates.

$\endgroup$
8
  • $\begingroup$ Interesting. If you Apply List to the DeleteDuplicates, results are peachy. $\endgroup$
    – ciao
    Feb 3, 2014 at 21:00
  • 1
    $\begingroup$ @Murta: Might want to change title to Row instead of DeleteDuplicates... a bit less alarming ;-) $\endgroup$
    – ciao
    Feb 3, 2014 at 21:14
  • 1
    $\begingroup$ please don't use the bugs tag when asking a question... if users (or WRI support/devs) confirm in the comments that it is indeed a weird behaviour/bug, then it can be retagged as such. $\endgroup$
    – rm -rf
    Feb 3, 2014 at 21:24
  • 2
    $\begingroup$ @rm-rf It was confirmed by Wolfram Support. We can tag it. $\endgroup$
    – Murta
    Feb 3, 2014 at 21:43
  • 1
    $\begingroup$ Appears to have been fixed in V10 $\endgroup$ Jul 16, 2014 at 4:09

2 Answers 2

12
$\begingroup$

Actually this happens when Row contains a packed array. DeleteDuplicates packs the array. Union doesn't. A simpler way to replicate the behaviour:

ToString@Row[Developer`ToPackedArray[{1,2}], ","]

(* ==> "Row[{1, 2}, ,]" *)

The default form used by ToString is OutputForm and the following also exhibits the problem:

OutputForm[Row[Developer`ToPackedArray[{1, 2}], ","]]

(* ==> Row[{1, 2}, ,] *)

I can reproduce it in both 8.0.4 and 9.0.1. I'd say it's a bug and you should report it to support.

$\endgroup$
2
  • 1
    $\begingroup$ Tks @Szabolcs, just reported (CASE:628688). Reproduced in V10 beta too. Let's see if final version 10 solves that. $\endgroup$
    – Murta
    Feb 3, 2014 at 21:14
  • $\begingroup$ Quick-draw analysis! +1 $\endgroup$
    – ciao
    Feb 3, 2014 at 22:14
5
$\begingroup$

As Szabolcs notes this occurs only when the first argument of Row is a packed array. In this case DeleteDuplicates returns a packed array while Union does not, which explains that difference:

<< Developer`
a = {1, 2};
b = Union @ a;
c = DeleteDuplicates @ a;
PackedArrayQ /@ {b, c}
{False, True}

One can see that this is not an issue of Row itself as these render as expected:

Row[b, ","]
Row[c, ","]
1, 2
1, 2

Rather, The variance occurs in OutputForm, which is normally not used in the Notebook interface, but which is the format that ToString defaults to:

Row[b, ","] // OutputForm
Row[c, ","] // OutputForm
1,2

Row[{1, 2}, ,]

I suspect that since Row was introduced in version 6, and it is ostensibly a 2D formatting function (it is not merely a string concatenation utility), someone forgot to update OutputForm to work in this edge case. For the case at hand one can easily avoid the problem by specifying the default Notebook format, StandardForm:

ToString[Row[b, ","], StandardForm]
ToString[Row[c, ","], StandardForm]
"1, 2"
"1, 2"
$\endgroup$
2
  • $\begingroup$ Much better use StandardForm instead of FromPackedArray as I was doing. Tks $\endgroup$
    – Murta
    Feb 4, 2014 at 10:41
  • 1
    $\begingroup$ Change my mind.. Now using StringJoin@Riffle[ToString /@ a, ","] $\endgroup$
    – Murta
    Feb 4, 2014 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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