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

MaxBy and other operations on empty Map.

4 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear all,
is there a way to execute a maxBy on Map, which do not throw exception when the map is empty, but returns a default value?
I end up doing:
val max = if (map.isEmpty) 0 else map.maxBy {something}
Don't you think it would be useful to have a method such as
val max = map.maxBy(0){something} 
I think it would be great if this will be included in the collection API...
Best Regards
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: MaxBy and other operations on empty Map.

maxOption or maxOrElse would be nice.
i prefer maxOption, just liek head & headOption

Am 02.02.2012 13:28, schrieb Edmondo Porcu:
> Dear all,
>
> is there a way to execute a maxBy on Map, which do not throw exception
> when the map is empty, but returns a default value?
>
> I end up doing:
>
> val max = if (map.isEmpty) 0 else map.maxBy {something}
>
> Don't you think it would be useful to have a method such as
>
> val max = map.maxBy(0){something}
>
> I think it would be great if this will be included in the collection
> API...
>
> Best Regards

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: MaxBy and other operations on empty Map.
You can do that with a fold.

  val max = map fold (0)((m,x) => m max {something_with_x})

If you are just calling methods, it's even shorter:

  val max = map.fold(0)(_ max _.myMethod)
  val max = map.maxBy(0)(_.myMethod)

It's _barely_ any shorter or clearer to have a dedicated method than to just use a fold.

I think we should just get rid of maxBy, since it cannot have any collection-specific shortcuts due to the mapping, and it isn't safe on empty collections as it is.

  --Rex


I would rather get rid of maxBy, which is unsafe and has no possible shortcuts on any collection, and instead have people use folds.

On Thu, Feb 2, 2012 at 7:28 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all,
is there a way to execute a maxBy on Map, which do not throw exception when the map is empty, but returns a default value?
I end up doing:
val max = if (map.isEmpty) 0 else map.maxBy {something}
Don't you think it would be useful to have a method such as
val max = map.maxBy(0){something} 
I think it would be great if this will be included in the collection API...
Best Regards

Harald Meland
Joined: 2011-06-22,
User offline. Last seen 42 years 45 weeks ago.
Re: MaxBy and other operations on empty Map.

On Thu, Feb 2, 2012 at 13:28, Edmondo Porcu wrote:
> Dear all,
>
> is there a way to execute a maxBy on Map, which do not throw exception when
> the map is empty, but returns a default value?
>
> I end up doing:
>
> val max = if (map.isEmpty) 0 else map.maxBy {something}

Since Map.maxBy returns a Tuple2, while your if-branch evaluates to an
Int: Are you really looking for a maxBy variant that works on
Map.empty.keys, .keySet or .keyIterator (or similarly for the
corresponding value methods of Map)?

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: MaxBy and other operations on empty Map.
Well no, I mistyped the code:

val max = if (map.isEmpty) 0 else map.maxBy {something}._2

Best

2012/2/2 Harald Meland <haraldme@gmail.com>
On Thu, Feb 2, 2012 at 13:28, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
> Dear all,
>
> is there a way to execute a maxBy on Map, which do not throw exception when
> the map is empty, but returns a default value?
>
> I end up doing:
>
> val max = if (map.isEmpty) 0 else map.maxBy {something}

Since Map.maxBy returns a Tuple2, while your if-branch evaluates to an
Int: Are you really looking for a maxBy variant that works on
Map.empty.keys, .keySet or .keyIterator (or similarly for the
corresponding value methods of Map)?
--
Harald

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