Should max and min have 0-arg implementations?

23 views
Skip to first unread message

Aaron Cohen

unread,
Jul 9, 2010, 1:18:34 PM7/9/10
to clo...@googlegroups.com
I noticed that http://clojure-examples.appspot.com/clojure.core/max has both (apply max [1 2 3 4]) -> 4 and (max []) -> [] (which I think is a poor example).

However, when attempting to add another example for (apply max []) which I expected to return nil, that instead it throws an exception.

I have to admit, I've never come across this in practice, but it seems like adding the overload would be nice for generality.

-- Aaron

Stuart Halloway

unread,
Jul 9, 2010, 1:57:05 PM7/9/10
to clo...@googlegroups.com
Once you walk down the path of "What should (max) return?" I think you won't want a default behavior.

Stu

P.S. Agreed that (max []) is a bad example.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Mike Meyer

unread,
Jul 9, 2010, 3:14:56 PM7/9/10
to clo...@googlegroups.com, stuart....@gmail.com
On Fri, 9 Jul 2010 13:57:05 -0400
Stuart Halloway <stuart....@gmail.com> wrote:

> Once you walk down the path of "What should (max) return?" I think you won't want a default behavior.
>
> Stu
>
> P.S. Agreed that (max []) is a bad example.

Given that max only works on numbers, then why doesn't (max []) throw
the same exception as (max [] [])? Or, for that matter, (max \a) throw
the same exception as (max \a \b \c)?

If max worked on any comparable (which is what I expected), then (max
A) always returning A makes sense, even if A isn't comparable. But
that behavior when max is defined as only working on numbers sure
seems like a bug.

<mike
--
Mike Meyer <m...@mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

Mark Engelberg

unread,
Jul 9, 2010, 3:58:22 PM7/9/10
to clo...@googlegroups.com
> Given that max only works on numbers, then why doesn't (max []) throw
> the same exception as (max [] [])? Or, for that matter, (max \a) throw
> the same exception as (max \a \b \c)?

Clojure tends not to guarantee any particular behavior for invalid
inputs. It might return an error, or it might give back a spurious
answer.

Most likely, the code for max just has a case that if it is only
passed a single input, then that is the answer. No error checking is
performed.

Reply all
Reply to author
Forward
0 new messages