Re: Missing in misc.e

new topic     » goto parent     » topic index » view thread      » older message » newer message

Juergen Luethje wrote:
> 
> CChris:
> 
> It's hard for me to understand your code.
> So please explain what the suggested global routines are doing, and why you
> think they are useful for a considerable number of Euphoria programmers.
> 
> Regards,
>    Juergen

1/ The abs() global functions computes the absolute value of any Euphoria
object. For atoms, it takes the negative of the atom if it is less than zero,
else leaves it alone. For sequences, it applies itself to any element which is
either a sequence or an atom less than 0 (other atoms needn't be touched), and
returns the transformed sequence.

2a/ find_min(sequence s) return the position of the minimum element in s
2b/ find_max(sequence s) return the position of the maximum element in s
2c/ get_min(sequence s) return the minimum element in s
2d/ get_max(sequence s) return the maximum element in s

These 4 routines rely on a single find_bound() function using different values
for the find_bound_mode control integer. This avoids repeating code and the
number of extra tests is minimal.

Internally, find_bound() starts with the first element of s (storing it as
candidate value and 1 as candidate index), then examines in turn each other
element in increasing index order. Whenever one is found which is less than or
greater than (depending on the current find_bound_mode) than the candidate value,
its value and position are stored as candidate result. When the sequence is
exhausted, either the recorded index or value is returned, depending again on
find_bound_mode.

3a/ min(object x,object y) returns the lesser of x and y
3b/ max(object x,object y) returns the greater of x and y.

4/ In all the above, the ordering relation being used is the one defined by the
compare() Euphoria function:
a is less than b if and only if compare(a,b)=-1

5/ These functions are very basic mathematical functions which have been
rewritten over and over again, to no benefit. Whenever you study some amount of
input, it is very usual to need the extreme values. The most frequent uses are on
sequences of atoms or integers, but since the compare() function works on every
pair of Euphoria object, there is no point in restricting the types of elements
in the min/max routines; that would only add overhead for no benefit. And the
absolute unction is even more basic than this.

As a result, a considerable number of programmers is already using home brew
equivalents of some or all of these routines, and it is high time they become
standard tools, so as to avoid everyone, specially newbies, the chore of
rewriting some of them, possibly with subtle differences or bugs.

Oh, and perhaps is it worthwhile to also add the
global function sign(object x)
   if atom(x) then 
      return compare(x,0.0)
   else
      for i=1 to length(x) do
         x[i]=sign(x[i])
      end for
      return x
   end if
end function

for reasons very similar to the ones exposed in 5/ above.

HTH
CChris

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu