Re: Missing in misc.e

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

Jason Gade wrote:
> 
> CChris wrote:
> > 
> > Not elegant, as the curly braces are not needed.
> > Confusing, because the min() notation to compare two objects is so
> > universal.
> > 
> > If we could overload symbols, ie having different routines share the same
> > name
> > with different signatures, because they do the same thing to different sorts
> > or forms of arguments, then both might be called min() or max() for more
> > convenience.
> > But I doubt this will be ever possible in Eu because it is weakly typed.
> > 
> > CChris
> 
> I still think it's elegant. Why have two functions that do the exact same
> thing?
> In fact, I'd write it to return an index as well:
> 
> }}}
<eucode>
> global function min(sequence s)
>     object x
>     integer index
> 
>     x = s[1]
>     for i = 2 to length(s) do
>         if x > s[i] then
>             x = s[i]
>             index = i
>         end if
>     end for
> 
>     return {x, index} -- depending on what is of more interest this order
>     could be swapped
> 
> end function
> </eucode>
{{{

> 
> Implement max() similarly or use one function with a flag value as you did
> somewhere
> up above.
> 
> Personally, I prefer weak-typing.
> 
> --
> "Any programming problem can be solved by adding a level of indirection."
> --anonymous
> "Any performance problem can be solved by removing a level of indirection."
> --M. Haertel
> "Premature optimization is the root of all evil in programming."
> --C.A.R. Hoare
> j.
Ack! I just copied Juergen's implementation from above and that might not work
(atom expected...)

Which kind of gets back to an earlier discussion.

global function min(sequence s)
    object x
    integer index
 
    x = s[1]
    for i = 2 to length(s) do
        if compare(x, s[i]) = 1 then
            x = s[i]
            index = i
        end if
    end for
 
return {x, index} -- depending on what is of more interest this order could
   be swapped

end function


--
"Any programming problem can be solved by adding a level of indirection."
--anonymous
"Any performance problem can be solved by removing a level of indirection."
--M. Haertel
"Premature optimization is the root of all evil in programming."
--C.A.R. Hoare
j.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu