1. Short-Circuiting: A real world example

WARNING:long message follows.
if you understand short circuit boolean logic, you can skip this
message.

This post attempts to expand on Jacques' prior post (a quite good
explanation of SC) with a real world example of why short circuiting
is a needed form of boolean expression/testing in coding.

My purpose here is not to inflame an already over discussed topic.
Rob has said he will implement SC in Euph...'nuff said there.
However, I have noticed a few members of the list that are
having difficulty with this topic, and ergo: this post.
Hopefully, it clarifies this rather arduous coding form for you.

In closing, please be gentle with that reply button, we do need to
move onto another topic here at Flamewars-R-Us... ;>
besides, I always get the last word :)

take care all--Hawke'

******************** official cut line....
btw, ever seen an official *paste* line? me
neither...sigh***************

include a_buncha_stuff_to_make_this_work.;)e
include and_dont_get_picky_on_typos.:>e
constant maxX=639, maxY=479
constant ri=1   --Red Index in sequence
constant gi=2   --Green Index...
constant bi=3   --Blue Index...
seq findcolor   --sequences of {red,green,blue}
seq replacecolor--describing truecolor pixels

--****** procedures **********
procedure changeallcolors(seq search, seq replace)
seq testcolor
for Xindex= 0 to maxX do
for Yindex= 0 to maxX do --total iteration of 307200 loops!
   testcolor = GetTruePixel(Xindex,Yindex) --returns {r,g,b}

   --version 1: no short circuiting
   --results in almost a MILLION if..then test!!!
   if   (testcolor[ri] = search[ri]) and
        (testcolor[gi] = search[gi]) and
        (testcolor[bi] = search[bi]) then
           --sets a pixel on the screen, in truecolor
           --mode, to the color specified (3rd argument)
           --which is in the format {red,green,blue}
           PlotTruePixel(Xindex,Yindex,replace)
   end if

   --version 2a: uses short circuiting
   --results in no more than 307200 iterations, a potential
   --savings of ~600000 loops! uses "scand", a notation
   --given in mailing list as a potential new identifier
   --representing "Short Circuit AND"
   if   (testcolor[ri] = search[ri]) scand
        (testcolor[gi] = search[gi]) scand
        (testcolor[bi] = search[bi]) then
           PlotTruePixel(Xindex,Yindex,replace)
   end if

   --the above equates to:
   --version 2b:
   if testcolor[ri] = search[ri] then   --i prefer this indentation
   if testcolor[gi] = search[gi] then   --for this situation
   if testcolor[bi] = search[bi] then   --lines up like #1 & 2a
        PlotTruePixel(Xindex,Yindex,replace)
   end if  --if there were elsif's...I'd prolly indent different
   end if
   end if

   --which version, 1 or 2, would you prefer to implement?
   --would you like waiting an extra 600000 loops???
   --note: i'm not asking about 2a or 2b either...
   --      they are both easily readable...etc to me
   --      and 2b is how to currently implement #1 SC'd

   --for the ones in the group that may think of this...
   --what about:
   --version 3:
   if same(testcolor, search) then  --same() from listserv msg
      PlotTruePixel(Xindex,Yindex,replace)
   end if
   --now, does compare (used by same) use short circuit????
   --yes? can you see why that may be???
   --no? errr... rob we needta talk... espresso? :)

end for
end for
end procedure

--******** main() ********
--use imagination here...
--e.g.. get user input for them to set the values...
findcolor   ={120,130,140}
replacecolor={150,160,170} --a little brighter color :)

changeallcolors(findcolor,replacecolor)

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu