Re: ABS

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

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01BDD75E.0595E850
        charset="iso-8859-1"

Lewis posted his offering for the ABS function:

>  function abs(object x)
>      return sqrt(x*x)
>  end function

I've avoided this discussion up to this point, but I couldn't resist the
bait. Getting a function as fast as possible is fine, but you probably want
to avoid this approach:

        - It can overflow for large numbers
        - It's non-intuitive

Naturally, I have my own submission:

        function abs( atom a )
                if a < 0 then
                        return -a
                else
                        return a
                end if
        end function

        function apply( object func, object o )
                -- recursively apply function to list

                -- convert name to identifier
                if sequence(func) then
                        func = routine_id( sequence )
                end if

                -- atom yet?
                if atom( o ) then
                        o = call_func( func, {o} )
                elsif length( o ) > 0 then
                        for i = 1 to length( o ) do
                                o[i] = apply( func, o[i] )
                        end for
                end if
                return o
        end function


So you can "simply" write:

        ? apply( "abs", {-1,0,1} )

Oh, yeah - because of the limitations of routine_id:

        - You have to define apply() AFTER abs()
        - You can't use apply() with native functions

And, of course, the code is a real speed demon!

Heh. blink


-- David Cuny

------_=_NextPart_001_01BDD75E.0595E850
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2232.0">
<TITLE>RE: ABS</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>Lewis posted his offering for the ABS =
function:</FONT>
</P>

<P><FONT SIZE=3D2>&gt;&nbsp; function abs(object x)</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return =
sqrt(x*x)</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp; end function</FONT>
</P>

<P><FONT SIZE=3D2>I've avoided this discussion up to this point, but I =
couldn't resist the bait. Getting a function as fast as possible is =
fine, but you probably want to avoid this approach:</FONT></P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>- It can =
overflow for large numbers</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>- It's =
non-intuitive</FONT>
</P>

<P><FONT SIZE=3D2>Naturally, I have my own submission:</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>function =
abs( atom a )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>if a &lt; 0 then</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>return =
-a</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>else</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>return =
a</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>end if</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>end =
function</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>function =
apply( object func, object o )</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>-- recursively apply =
function to list</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>-- convert name to =
identifier</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>if sequence(func) =
then</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>func =
=3D routine_id( sequence )</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>end if</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>-- atom yet?</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>if atom( o ) then</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>o =3D =
call_func( func, {o} )</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>elsif length( o ) &gt; 0 =
then</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>for i =
=3D 1 to length( o ) do</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <FONT =
SIZE=3D2>o[i] =3D apply( func, o[i] )</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>end =
for</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>end if</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>return o</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>end =
function</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>So you can &quot;simply&quot; write:</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>? apply( =
&quot;abs&quot;, {-1,0,1} )</FONT>
</P>

<P><FONT SIZE=3D2>Oh, yeah - because of the limitations of =
routine_id:</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>- You have =
to define apply() AFTER abs()</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=3D2>- You =
can't use apply() with native functions</FONT>
</P>

<P><FONT SIZE=3D2>And, of course, the code is a real speed =
demon!</FONT>
</P>

<P><FONT SIZE=3D2>Heh. blink</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>-- David Cuny</FONT>
</P>

</HTML>
------_=_NextPart_001_01BDD75E.0595E850--

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

Search



Quick Links

User menu

Not signed in.

Misc Menu