1. Hints & Tips

Since I may lose my net access in about a month's time, I thought I'd
start a worthwhile thread for those of us Euphorians who just like hobby
programming with Euphoria.

Maybe Rob "I *am* Euphoria" Craig could include everybody's entries on
the fairly unused Programming Tips page...

1) These two lines are legal in Euphoria:
        if a = 7 and b = 7 then ...
        if a = 7 or b = 7 then ...

   but this isn't:
        if a = 7 xor b = 7 then ... -- a = 7 or b = 7, but *not* both!
                 ^^^
   You could get round this by coding:
        if a = 7 or b = 7 and a != b then ...

   but there's a shorter way:
        if (a = 7) != (b = 7) then ... -- Trust me, this works!

2) Ever coded something like this?:
        flag = 0
        for i = 1 to length(seq)
            if seq[i] = 'a' or seq[i] = 'b' or seq[i] = 'z' then
                flag = 1
                --exit --maybe
            end if
        end for

        if flag then ... etc.

   But if 'seq' was an atom you'd do this:
        if seq = 'a' or seq = 'b' or seq = 'z' then ...

   So combine the two!:
        if find(1, (seq = 'a' or seq = 'b' or seq = 'z')) then ... etc.
            -- The find(1,... represents finding a truth value
            -- don't confuse it with puts(1,...!

   This method, however, does have a *small* problem. If there would have
   been (or is) an 'exit' in the for-loop, the for-loop may actually be
   quicker than Euphoria checking the whole of the sequence.
   [For 'a's 'b's and 'z's in this case].

3) If you really *have* to use the for-loop-with-exit above (for speed),
   try changing the if statement to:
        flag = 0
        for i = ...
            if find(seq[i], "abz") then ... etc.

4) Expanding from the above:
   If you have to code a loop that runs through a sequence an element
   at a time, there's probably a way of getting Euphoria to do the hard
   work for you by using a function that treats sequences like atoms.

Happy coding!
Carl

--
Carl R White
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
Url......: http://www.bigfoot.com/~cyrek/
In my last few days on the net, I have room for a witty quote. Irony. :S

new topic     » topic index » view message » categorize

2. Hints & Tips

File length function

function file_length(integer file_num) -- assumed file already open as
-- binary, device number file_num
    integer seek_success
    seek_success =3D seek(file_num,-1)
    file_length =3D where(file_num)
    seek_success =3D seek(file_num,0)
    return file_length
end function

--Alan
  =

new topic     » goto parent     » topic index » view message » categorize

3. Re: Hints & Tips

I find these more elegant to read and code,
thus, they have become staples in my toolbox:

function IsSame(object first,object second)
return compare(first,second) = 0
end function

function IsNotSame(object first,object second)
return not ( compare(first,second)=0 )
end function
=================

a relatively accurate, machine independent
routine for waiting. accepts fractional seconds
as well...
use: Delay(10)  waits 10 seconds
     Delay(0.2) waits 2 tenths of a second

procedure Delay(atom seconds)
--assumes tick_rate() is default
atom now
  now = time()
  while (time() - now) < seconds do
  end while
end procedure

example use:
procedure Beep()
--goes "beep" in an ever so pleasant way, informing you
--(most likely) that you just screwed up :)
  sound(500) Delay(0.2) sound(0)
end procedure
========================

new topic     » goto parent     » topic index » view message » categorize

4. Re: Hints & Tips

Carl R. White wrote:
> 2) Ever coded something like this?:
>         flag = 0
>         for i = 1 to length(seq)
>             if seq[i] = 'a' or seq[i] = 'b' or seq[i] = 'z' then
>                 flag = 1
>                 --exit --maybe
>             end if
>         end for
>
>         if flag then ... etc.
>
> 4) Expanding from the above:
>    If you have to code a loop that runs through a sequence an element
>    at a time, there's probably a way of getting Euphoria to do the hard
>    work for you by using a function that treats sequences like atoms.

it should cause euphoria to do the hard work for you if you replaced all
of the above
with this one single line, which reads rather well i think, and *should*
be
exceptionally fast, perhaps even faster than walking all the way thru
the sequence
an element at a time since its pseudo-slicing and examining a sequence
all at once.
note: find returns 0 if not found or a number > 0 indicating the
location
        within the sequence where target is found. now, since not-zero is
TRUE...

flag = ( find('a',seq) or find('b',seq) or find('z',seq) )

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu