Re: Standard library praise and challenge
- Posted by DerekParnell (admin) Sep 13, 2011
- 3288 views
SDPringle said...
I am seeing most people describing the functions max and min I wrote myself. I ask you though, what should happen when you pass an empty sequence?
I give you an empty bag and ask you to pull out the largest object. You can't pull out any object so therefore this is an error. The results are defined - you pull nothing out, which is of course neither the biggest nor the smallest, thus we have a problem - also known as an error situation. One way to signal this error is to just give back the bag.
It's clear that math:max() is intended to only work with numbers, but I think we need max/min functions that work with objects too.
public function max(object a) object b,c if atom(a) then return a end if if length(a) = 0 then return a end if b = a[1] for i = 2 to length(a) do c = a[i] if compare(c, b) > 0 then b = c end if end for return b end function