- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Iterating over a map in the right order.
Mon, 2012-02-06, 11:54
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
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
Mon, 2012-02-06, 15:21
#2
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
>
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