1. Re: [E - 19/20 Mar 97] any() and all()

E::>Jef Zeitlin provided us with some very nice routines, all() and any().

 Thank you for the compliment.  I enjoy writing code, and will
 cheerfully write little nothing routines that might be useful
 to someone.  Right now, I'm checking my Disorganized
 Encyclopedic Collection of Strange and Obscure Programming
 Languages for interesting functionality that could be
 implemented in Euphoria; I'll have some more routines soon.

 Incidentally, there's _two_ 'f's in 'Jeff'.

E::>global function all(object o)
 ::>    if atom(o) then
 ::>        return o        -- that is the letter, not 0(ZERO)
 ::>    end if
 ::>    for i = 1 to length(o) do
 ::>        if not o[i] then
 ::>            return 0    -- this *is* a ZERO
 ::>        end if
 ::>    end for
 ::>    return 1
 ::>end function    -- all()
 ::>--------------------------------------------------------
 ::>global function any(object o)
 ::>    if atom(o) then
 ::>        return o
 ::>    end if
 ::>    for i = 1 to length(o) do
 ::>        if i[o] then           <<<- _my_ copy says o[i]! :) JZ
 ::>            return 1
 ::>        end if
 ::>    end for
 ::>    return 0
 ::>end function    -- any
 ::>--------------------------------------------------------

E::>I've been checking these out, but it seems to me you can speed them up (at
 ::>least in a lot of cases), by replacing the loops 'for..do /if..then... end
 ::>if /end for' with a simple 'if find(0, o) then...' and 'if find(1, o)
 ::>then...'.

 Not necessarily.  My examples had a boolean expression as the
 object parameter, but it's not required that they be all 0 and
 1 - consider the case where you only want to check for
 non-zero values in the sequence itself:

        x = {2,4,6,8,"EUPHORIA IS REALLY GREAT"}
        if any(x) then          -- identical to any(x!=0)
            ...                 -- any(x) will be true
        end if

 ::>The sequence o, that is used inside the routines, is of the same length as
 ::>the sequence that is passed to the routines, but it only contains
 ::>0's(FALSE) and 1's(TRUE).

 No, look at it again - the sequence o _is_ whatever is passed
 in.  The names were chosen so that it would "read naturally" if
 you were passing a vector of boolean results, but just as you
 don't need to pass a boolean in as the test of a regular if,
 you don't need it here.

 ::>It seems to me that reading the sequence using 'find' is more elegant and
 ::>probably quicker than using a loop inside a loop.

 I'm only using a single loop, and exiting as soon as I have a
 conclusive result.  I'm not sure I can see how to speed that
 up, and still have it be as general as it is.  Wait, yes there
 is - you're right:  The test in all() becomes "if not
 find(0,o)"; the test in any() becomes "if find(0,not o)".  Good
 eye, but you dropped the ball after getting your glove on it.
 Thanks for the pointer; I've made a note of it, and will update
 the routines and repost them to the list.

 ::>I tested my suggested improvement to find the square of 99999 in a
 ::>100000-element sequence of squares. (Pretty useless again). In this case it
 ::>took my Pentium 75 with 8Mb 0.16 seconds, whereas Jef's code took twice as
 ::>much.

 100,000 comparisons in 0.32 seconds on a slow P5 and you're
 complaining about the speed?!  :)

 ::>But when I began testing it with 200000 and 300000 elements, the speed
 ::>difference decreased, I think maybe because most of the time was spend
 ::>writing to and reading from the hard disk.
 ::>Can someone with more RAM test this?

 Now you've got me curious; I'll have to check this on my
 P120/24MB!

E::>PS. I wander how long this message will take to get to the Listserver. Last
 ::>time it took 2 whole days. I'm posting this one now on March 20. In the USA
 ::>(Westcoast) it should be just 3 a.m.

 I downloaded it on 3/21, which means that it arrived some time
 between 17:00 on 3/20 and 17:00 on 3/21.

 =========================================================================
Jeff Zeitlin                                      jeff.zeitlin at execnet.com
---
 ~ OLXWin 1.00b ~ Happiness is your favorite program moving to Windows

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu