- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
MaxBy and other operations on empty Map.
Thu, 2012-02-02, 13:28
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
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
Thu, 2012-02-02, 19:21
#2
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:
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
Thu, 2012-02-02, 20:41
#3
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)?
Thu, 2012-02-02, 20:51
#4
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>
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
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