Re: Standard library praise and challenge
- Posted by DerekParnell (admin) Sep 22, 2011
- 2617 views
If max({}) fails what of max({ {} }). Are you say max() can only return a number?
I think that max()/min() functions only make sense if there is a minimum of two items to compare. And I certainly think the current recursive aspect of the math library functions is a mistake.
Now one might argue that an edge case is when a list has only a single item, in which case that item is returned. But when a list has no items, returning which of those non-existent items is the max/min is meaningless. In such a case, the function should either return ...
- a value that unambiguously indicates that an empty list was passed
- crash
- throw an exception
For now, I prefer the second option; crash the application.
There is no unambiguous value that can be returned with the current Euphoria. We can't use {} or NaN, because both of those might actually be one of the items in a non-empty list.
And of course, Euphoria doesn't have exceptions yet.
If your application has a real case in which an empty list can be validly passed to the function, then you should test for that explicitly as I suspect it would be a rare thing in general.