This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Iterating over a map in the right order.

2 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear all,
I have a non ordered map and I would like to iterate over it in an ordered fashion on keys.
my map is a Map[Int,Double]
I tried the following :
 swaps.toIndexedSeq.sortBy{case (maturity,rate)=>maturity    }.
However I get the following error : 
error: missing parameter type for expanded functionThe argument types of an anonymous function must be fully known. (SLS 8.5)Expected type was: ? => ?swaps.toIndexedSeq.sortBy{
I am very surprised by that...Do you have any idea of why the compiler cannot infer the type of the tuple ? 
Also...is there maybe a better way to iterate over a map in the correct order?
Thank you
Edmondo
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Iterating over a map in the right order.

random thought: try sortBy(_._1)

-------- Original-Nachricht --------
> Datum: Mon, 6 Feb 2012 11:54:38 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Iterating over a map in the right order.

> Dear all,
>
> I have a non ordered map and I would like to iterate over it in an ordered
> fashion on keys.
>
> my map is a Map[Int,Double]
>
> I tried the following :
>
> swaps.toIndexedSeq.sortBy{
> case (maturity,rate)=>maturity
> }.
>
> However I get the following error :
>
> error: missing parameter type for expanded function
> The argument types of an anonymous function must be fully known. (SLS 8.5)
> Expected type was: ? => ?
> swaps.toIndexedSeq.sortBy{
>
> I am very surprised by that...Do you have any idea of why the compiler
> cannot infer the type of the tuple ?
>
> Also...is there maybe a better way to iterate over a map in the correct
> order?
>
> Thank you
>
> Edmondo

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Iterating over a map in the right order.

That's because toIndexedSeq is parameterized on Scala 2.9.1 and
previous versions, which causes the type inference to fail further on.
This was fixed on trunk.

As an aside, tuples are automatically ordered, so you can just call
".sorted", which doesn't require any functions, to make it work.
Otherwise, split it in two lines:

val seq = swaps.toIndexedSeq
val sorted = seq.sortBy(...)

Or use toSeq instead of toIndexedSeq, since toSeq is not parameterized.

On Mon, Feb 6, 2012 at 08:54, Edmondo Porcu wrote:
> Dear all,
>
> I have a non ordered map and I would like to iterate over it in an ordered
> fashion on keys.
>
> my map is a Map[Int,Double]
>
> I tried the following :
>
>  swaps.toIndexedSeq.sortBy{
> case (maturity,rate)=>maturity
>     }.
>
> However I get the following error :
>
> error: missing parameter type for expanded function
> The argument types of an anonymous function must be fully known. (SLS 8.5)
> Expected type was: ? => ?
> swaps.toIndexedSeq.sortBy{
>
> I am very surprised by that...Do you have any idea of why the compiler
> cannot infer the type of the tuple ?
>
> Also...is there maybe a better way to iterate over a map in the correct
> order?
>
> Thank you
>
> Edmondo
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland