28 September 2009

Scala: Map of Lists, Part II

A couple of posts ago, I talked about an attempt to create sort of a group by method for partitioning an existing list.

Well, I fussed over it for a while longer and came up with this:

Instead, I created an implicit function, and removed the projection part (C to B) to get the following:


implicit def listenhancer[C](list:List[C]) = new {
def groupby[A](keyresolver:C=>A) = {
Map[A,List[C]](
(list map { keyresolver(_) } removeDuplicates)
map { key => (key, list filter { key equals keyresolver(_) }) }
: _*
)
}
}


Now, I know that some in the Scala community don't like implicit functions. For now, I think that they create very clean-looking code, and so they are still on my "Good Coding Practice" list.

Anyway, one of the things that I noticed about the other is that the keys list had a whole bunch of duplicates, and so the valueresolver method was getting called too many times. So, I added the 'removeDuplicates' call. Second, I decided that, to keep the goal of this method succinct, I wasn't going to support projection unless there was a compelling reason.

So, now, I can just call it like this:


val assetsMap = assets groupby { asset => asset.getName.substring(0, asset.getName.indexOf("_file")) }


Doesn't that look a lot nicer?