12

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Murta
  • 26,275
  • 6
  • 76
  • 166
  • Interesting. If you Apply List to the DeleteDuplicates, results are peachy. – ciao Feb 03 '14 at 21:00
  • Confirmed in 8.0.4 and 9.0.1 on OS X. Very weird and slightly disturbing. I guess you should report it to support. – Szabolcs Feb 03 '14 at 21:04
  • 1
    @Murta: Might want to change title to Row instead of DeleteDuplicates... a bit less alarming ;-) – ciao Feb 03 '14 at 21:14
  • @rasher done! But Row to me is alarming to :). I use it to inject information into some SQL strings, and now I have to Unpack it using FromPackedArray (very annoying). – Murta Feb 03 '14 at 21:19
  • 1
    please don't use the [tag: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. – rm -rf Feb 03 '14 at 21:24
  • 2
    @rm-rf It was confirmed by Wolfram Support. We can tag it. – Murta Feb 03 '14 at 21:43
  • @Murta Thanks. I did not remove the tag when I edited, because there was already some discussion in the comments by then. I meant it more for future reference :) The main reason we have this rule is because sometimes a certain behaviour might appear to be a bug, but it actually might be documented or expected (but unknown to the user). – rm -rf Feb 03 '14 at 21:59
  • 1
    Appears to have been fixed in V10 – Mike Honeychurch Jul 16 '14 at 04:09

2 Answers2

12

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
5

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"
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371