1. RE: Case/switch

SR.Williamson wrote:
> So there are a lot of 
> "if button1 = leftedge or button2 = rightedge" kind of stuff.
> It got really tedious. The only think I could think was "This
> would be so much easier with a Case statement".

I've developed a game where, not only do I have to check for "being on 
the edge" of the playfield, but also if next to a pit, next to a wall, 
next to another player, etc., etc... To me, it wasn't so tedious, so 
your example, to me, is even easier/more not-so-tedious.

new topic     » topic index » view message » categorize

2. RE: Case/switch

> -----Original Message-----
> From: SR.Williamson [mailto:writeneu at hotmail.com]

> Kat brought up case/switch. I'd love to see this become part of the
> language.
>
> I was writing an artificial life program for Windows.
> Basically, it was
> a bunch of buttons in a grid. If a button was "hot", the
> program would
> check to see whether the button in the row above and to
> either side was
> "hot" to determine whether to make that button itself "hot".
>
> Well, as you can imagine, that works fine for the buttons in
> the middle
> of the grid. But when you get to the buttons in the grid
> above, you have
> to check to see if a button is at a window edge. So there are
> a lot of
> "if button1 = leftedge or button2 = rightedge" kind of stuff. It got
> really tedious. The only think I could think was "This would
> be so much
> easier with a Case statement".
>
> Is there some easy way to do that that I'm missing?

My typical method for eliminating large if/elsif (ie, case) structures is
using routine_id.  Then you can set up your condition[s] and the code to
handle said conditions in an associative list.  It's easy to add or delete
cases this way, and you don't end up with a massive piece of code for which
you can't easily follow the flow/decision process.

Your example might look something like:

sequence condition, result

condition = { middle, leftedge, rightedge, topedge, bottomedge }
result = { routine_id("do_middle"),.....}

Then, for your decision:

case = find( situation, condition )
call_proc( result[case] )


Matt Lewis

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

3. RE: Case/switch

On 15 Feb 2002, at 13:46, Matthew Lewis wrote:

> 
> 
> > -----Original Message-----
> > From: SR.Williamson [mailto:writeneu at hotmail.com]
> 
> > Kat brought up case/switch. I'd love to see this become part of the
> > language.
> >
> > I was writing an artificial life program for Windows.
> > Basically, it was
> > a bunch of buttons in a grid. If a button was "hot", the
> > program would
> > check to see whether the button in the row above and to
> > either side was
> > "hot" to determine whether to make that button itself "hot".
> >
> > Well, as you can imagine, that works fine for the buttons in
> > the middle
> > of the grid. But when you get to the buttons in the grid
> > above, you have
> > to check to see if a button is at a window edge. So there are
> > a lot of
> > "if button1 = leftedge or button2 = rightedge" kind of stuff. It got
> > really tedious. The only think I could think was "This would
> > be so much
> > easier with a Case statement".
> >
> > Is there some easy way to do that that I'm missing?
> 
> My typical method for eliminating large if/elsif (ie, case) structures is
> using routine_id.  Then you can set up your condition[s] and the code to
> handle said conditions in an associative list.  It's easy to add or delete
> cases this way, and you don't end up with a massive piece of code for which
> you
> can't easily follow the flow/decision process.
> 
> Your example might look something like:
> 
> sequence condition, result
> 
> condition = { middle, leftedge, rightedge, topedge, bottomedge }
> result = { routine_id("do_middle"),.....}
> 
> Then, for your decision:
> 
> case = find( situation, condition )
> call_proc( result[case] )

Shouldn't this be in the official Eu help files? Karl, if this can be done in
two
lines of Eu, wouldn't it be easy as a command alias in the native code?

Kat

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

4. RE: Case/switch

-------Phoenix-Boundary-07081998-

Hi Kat, you wrote on 2/14/02 11:21:23 AM:

>
>On 15 Feb 2002, at 13:46, Matthew Lewis wrote
> > -----Original Message-----
> > From: SR.Williamson [mailto:writeneu at hotmail.com]
>
> Your example might look something like:
>
> sequence condition, result
>
> condition = { middle, leftedge, rightedge, topedge, bottomedge }
> result = { routine_id("do_middle"),.....}
>
> Then, for your decision:
>
> case = find( situation, condition )
> call_proc( result[case] )
>

>Shouldn't this be in the official Eu help files? Karl, if this can be
done
>in two
>lines of Eu, wouldn't it be easy as a command alias in the native code?
>

What you really have here is a single function call:
    switch (situation, condition, result)
which does the 2 lines in question. One improvement might be
to combine the condition and result arrays into one:
   sequence mycases = {{middle, routine_id (do_middle)}
                       , (top, routine_id (do_proc)}
                       ... }
Then write a switch procedure:
    switch (situation, mycases)

All of which, to my mind, shows the versatility of
Euphoria but misses the point of the real
switch statement-- the close coupling between the
'case' and the executed code.

For source code hackers I would suggest two
approaches:

1) Simple renaming for clarity:
   switch var_x
   case 3:
   case 4:
   ...
   end switch

Which would be exactly equivalent to:
   if var_x = 3 then
   elsif var_x = 4 then
   ...
   end if

2) The addition of a new opcode which does a direct
   branch through a table.
   There are some tough issues in the generation of
   a table -- for instance:
     switch x
     case 1:
     case 120000:

Karl

-------Phoenix-Boundary-07081998---

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

Search



Quick Links

User menu

Not signed in.

Misc Menu