Sorted structures lose their ordering when mapped

Tobin Yehle <[email protected]> Mon, 28 Nov 2016 20:14:06 -0800 (PST)
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
------=_Part_1727_2122738624.1480392846393
Content-Type: multipart/alternative; 
	boundary="----=_Part_1728_1186045185.1480392846394"

------=_Part_1728_1186045185.1480392846394
Content-Type: text/plain; charset=UTF-8

Not sure if I'm really posting this in the right place, but:

Calling map on a collection that requires an implicit ordering results in 
some surprising behavior. For example:

scala> val set = SortedSet(1,2,3)(ord=Ordering.Int.reverse)
set: scala.collection.SortedSet[Int] = TreeSet(3, 2, 1)


scala> set.firstKey == set.map(identity).firstKey
res0: Boolean = false

My feeling is that mapping the identity function over any collection should 
not change anything.

In this case it is clear what is happening. The CanBuildFrom provided by 
SortedSet is pulling an Ordering instance out of predef (Ordering.Int) 
instead of using the ordering from the origin collection.
scala> set.toList
res1: List[Int] = List(3, 2, 1)


scala> set.map(identity).toList
res2: List[Int] = List(1, 2, 3)

scala> set.ordering
res3: Ordering[Int] = scala.math.Ordering$$anon$4@3ff3275b

scala> set.map(identity).ordering
res4: Ordering[Int] = scala.math.Ordering$Int$@7fa85a55

I think this can be fixed by providing a more specific CanBuildFrom 
instance to be used when mapping between two sorted collections that 
contain the same type.

i.e. Provide a CanBuildFrom[Sorted[K, _], K, Sorted[K, _]] somewhere that 
has precedence over the more generic usual CanBuildFrom[CC[_], A, CC[A]]

I'm not sure how it could be done, but the static overloading resolution 
rules make this possible, I think.

-- 
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

------=_Part_1728_1186045185.1480392846394
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Not sure if I&#39;m really posting this in the right =
place, but:</div><div><br></div>Calling map on a collection that requires a=
n implicit ordering results in some surprising behavior. For example:<div><=
div><br></div><div class=3D"prettyprint" style=3D"background-color: rgb(250=
, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-=
width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y">scala</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&g=
t;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> val </s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">set</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;=
" class=3D"styled-by-prettify">SortedSet</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">(</span><span style=3D"color: #066;" class=3D=
"styled-by-prettify">1</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">,</span><span style=3D"color: #066;" class=3D"styled-by-prettif=
y">2</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</spa=
n><span style=3D"color: #066;" class=3D"styled-by-prettify">3</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">)(</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">ord</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #606;" =
class=3D"styled-by-prettify">Ordering</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">.</span><span style=3D"color: #606;" class=3D"st=
yled-by-prettify">Int</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">reverse</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span=
><span style=3D"color: #008;" class=3D"styled-by-prettify">set</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> scala</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify">collection</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">.</span><span style=3D"color: #606;" class=3D=
"styled-by-prettify">SortedSet</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">[</span><span style=3D"color: #606;" class=3D"styled-by=
-prettify">Int</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">]</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">TreeSet</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #=
066;" class=3D"styled-by-prettify">3</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-pr=
ettify">2</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #066;" class=3D"styled-by-prettify">1</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br><br><br>scala</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">set</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">firstKey </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">=3D=3D</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify=
">set</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">map</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">identity</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">).</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">firstKey<br>res0</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" =
class=3D"styled-by-prettify">Boolean</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>false</span></div></code></div><div><br></div></div><div>My feeling is tha=
t mapping the identity function over any collection should not change anyth=
ing.</div><div><br></div><div>In this case it is clear what is happening. T=
he CanBuildFrom provided by SortedSet is pulling an Ordering instance out o=
f predef (Ordering.Int) instead of using the ordering from the origin colle=
ction.</div><div class=3D"prettyprint" style=3D"background-color: rgb(250, =
250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-wi=
dth: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D=
"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettify">=
scala</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">set</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">toList<br>res1</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" clas=
s=3D"styled-by-prettify">List</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">[</span><span style=3D"color: #606;" class=3D"styled-by-=
prettify">Int</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">]</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D=
"color: #606;" class=3D"styled-by-prettify">List</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #066;" =
class=3D"styled-by-prettify">3</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify=
">2</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #066;" class=3D"styled-by-prettify">1</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br><br><br>scala</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">set</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">map</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">identity=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">).</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify">toList<br>res2</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #606;" class=3D"styled-by-prettify">List</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">[</span><span style=3D"color: #606;" c=
lass=3D"styled-by-prettify">Int</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">]</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #606;" class=3D"styled-by-prettify">List</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #066;" class=3D"styled-by-prettify">1</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"=
styled-by-prettify">2</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">3</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><span clas=
s=3D"styled-by-prettify"><font color=3D"#000000"><div class=3D"subprettypri=
nt">scala&gt; set.ordering</div><div class=3D"subprettyprint">res3: Orderin=
g[Int] =3D scala.math.Ordering$$anon$4@3ff3275b</div><div class=3D"subprett=
yprint"><br></div><div class=3D"subprettyprint">scala&gt; set.map(identity)=
.ordering</div><div class=3D"subprettyprint">res4: Ordering[Int] =3D scala.=
math.Ordering$Int$@7fa85a55</div></font></span></div></code></div><div><div=
><br></div><div>I think this can be fixed by providing a more specific CanB=
uildFrom instance to be used when mapping between two sorted collections th=
at contain the same type.</div></div><div><br></div><div>i.e. Provide a Can=
BuildFrom[Sorted[K, _], K, Sorted[K, _]] somewhere that has precedence over=
 the more generic usual CanBuildFrom[CC[_], A, CC[A]]</div><div><br></div><=
div>I&#39;m not sure how it could be done, but the static overloading resol=
ution rules make this possible, I think.</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">scal=
[email protected]</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

------=_Part_1728_1186045185.1480392846394--

------=_Part_1727_2122738624.1480392846393--