java.lang.UnsupportedOperationException: empty.max

2,406 views
Skip to first unread message

Nils Kilden-Pedersen

unread,
Aug 9, 2013, 8:50:05 AM8/9/13
to scala-user
Just saw this nasty bugger again.

Dear god, why was this not made an Option?

Oliver Ruebenacker

unread,
Aug 9, 2013, 9:22:46 AM8/9/13
to Nils Kilden-Pedersen, scala-user
Hello,
For roughly the same reason that integer division doesn't return
Option[Int], rather than throwing an exception if one tries to divide
by zero? To keep simple things simple?

As a math modeler, I don't want my math algorithms to be cluttered
with Option-handling.

That said, I wouldn't mind if there was an additional method just
like max but returning Option.

Plus, another type of exception might be helpful, other than Unsupported.

Take care
Oliver

--
Head of Systems Biology Task Force at PanGenX (http://www.pangenx.com)
Any sufficiently advanced technology is indistinguishable from magic.

Dennis Haupt

unread,
Aug 9, 2013, 9:57:56 AM8/9/13
to Oliver Ruebenacker, Nils Kilden-Pedersen, scala-user
just as there are head and headOption, there should be max and maxOption.
i don't want to handle the empty list special case every time
 
Gesendet: Freitag, 09. August 2013 um 15:22 Uhr
Von: "Oliver Ruebenacker" <cur...@gmail.com>
An: "Nils Kilden-Pedersen" <ni...@kilden-pedersen.net>, scala-user <scala...@googlegroups.com>
Betreff: Re: [scala-user] java.lang.UnsupportedOperationException: empty.max
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

 

Vlad Patryshev

unread,
Aug 9, 2013, 10:34:41 AM8/9/13
to Nils Kilden-Pedersen, scala-user
I would suggest that max is not an option. If you have a type (empty[T]), then this fold (I mean max) should return the neutral element regarding max(x,y), that is, the minimal element of the (partial) order of T, if one exists; if it does not, then it makes sense to throw an exception.
Same as with other monoidal operations, e.g. empty.sum is a zero, not an exception.

This would be the right mathematical answer.

Thanks,
-Vlad


On Fri, Aug 9, 2013 at 5:50 AM, Nils Kilden-Pedersen <ni...@kilden-pedersen.net> wrote:
Just saw this nasty bugger again.

Dear god, why was this not made an Option?

--

Vlad Patryshev

unread,
Aug 9, 2013, 10:36:56 AM8/9/13
to Dennis Haupt, Oliver Ruebenacker, Nils Kilden-Pedersen, scala-user
Head is a different story; here we have a semigroup, not a monoid. There's no neutral element.

Thanks,
-Vlad

Oliver Ruebenacker

unread,
Aug 9, 2013, 10:43:49 AM8/9/13
to Vlad Patryshev, Nils Kilden-Pedersen, scala-user
Hello,

scala> List(1.0, Double.NaN).max
res1: Double = NaN

scala> List(Double.NaN, 1.0).max
res2: Double = 1.0

:)

Take care
Oliver

Vlad Patryshev

unread,
Aug 9, 2013, 11:07:03 AM8/9/13
to Oliver Ruebenacker, Nils Kilden-Pedersen, scala-user
Everything's broken in this world...

Thanks,
-Vlad

Seth Tisue

unread,
Aug 9, 2013, 11:46:05 AM8/9/13
to scala-user
On Fri, Aug 9, 2013 at 9:57 AM, Dennis Haupt <h-s...@gmx.de> wrote:
> just as there are head and headOption, there should be max and maxOption.
> i don't want to handle the empty list special case every time

fwiw:

scala> util.Try(List[Int]().max).toOption
res2: Option[Int] = None

scala> util.Try(List[Int](1, 2).max).toOption
res3: Option[Int] = Some(2)

Dennis Haupt

unread,
Aug 9, 2013, 12:12:12 PM8/9/13
to Seth Tisue, scala-user
you are underestimating my laziness. for me, it has to be at least an implicit conversion which adds the method
 
Gesendet: Freitag, 09. August 2013 um 17:46 Uhr
Von: "Seth Tisue" <se...@tisue.net>
An: scala-user <scala...@googlegroups.com>
Betreff: Re: Re: [scala-user] java.lang.UnsupportedOperationException: empty.max

Nils Kilden-Pedersen

unread,
Aug 9, 2013, 12:31:53 PM8/9/13
to Seth Tisue, scala-user
The problem with this is that it assumes the user knows to expect an exception, in which case this wouldn't be an issue.

Another way would be to force a default.

Som Snytt

unread,
Aug 9, 2013, 2:00:07 PM8/9/13
to Nils Kilden-Pedersen, Seth Tisue, scala-user

Scala could have innovated by calling them "expections".

Who said "expect the unexpected"?  Was it Knuth? Wirth?

Or Heraclitus (http://en.wikiquote.org/wiki/Heraclitus) --

"He who does not expect the unexpected will not find it, since it is trackless and unexplored."

Apparently they didn't have stack traces back then.

Oscar Wilde made it a bon mot:

“To expect the unexpected shows a thoroughly modern intellect.”

http://www.goodreads.com/quotes/191285-to-expect-the-unexpected-shows-a-thoroughly-modern-intellect

Michal Politowski

unread,
Aug 12, 2013, 5:06:32 AM8/12/13
to scala-user
Apparently some things take time to become broken.

Welcome to Scala version 2.9.2 (OpenJDK Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> List(1.0, Double.NaN).max
res0: Double = NaN

scala> List(Double.NaN, 1.0).max
res1: Double = NaN
Reply all
Reply to author
Forward
0 new messages