For/While/Foreach

Suggest changes and improvements to Algodoo.

For/While/Foreach

Postby Dakta » Thu Mar 11, 2010 8:36 pm

I suggest that we get real for/while/foreach functions! Going without them has been silly. Having to create a function to run through an array and apply functions to the items is silly. Along with all the other stuff you can do with real for/while/foreach functions.

Also, it would be appreciated if we could have elseif statements, so we can avoid the ridiculous nested conditionals we've had to use. Not only are they messy, they're ugly and a pain in the a$$.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: For/While/Foreach

Postby Dakta » Sat Mar 27, 2010 1:54 am

Okay, I know a lot of you guys support this idea, even link0007, but it won't get anywhere if you don't post replies!

Is my suggestion so perfect that it needs no other comments to prove its point?

Come on people!
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: For/While/Foreach

Postby nsmith » Sat Mar 27, 2010 3:03 am

i agree even though we got of on the wrong foot lol. (take my support or not its all the same to me :D)
Email me "nsmith0723@gmail.com"
nsmith
 
Posts: 48
Joined: Sun Mar 21, 2010 6:31 pm

Re: For/While/Foreach

Postby Mystery » Sat Mar 27, 2010 3:12 am

Damn it. This topic was a lot funnier when no one posted.
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: For/While/Foreach

Postby nsmith » Sat Mar 27, 2010 3:22 am

i can delete it if a want?!?!
Email me "nsmith0723@gmail.com"
nsmith
 
Posts: 48
Joined: Sun Mar 21, 2010 6:31 pm

Re: For/While/Foreach

Postby Chronos » Sat Mar 27, 2010 1:17 pm

I probably won't use this, but I support just so you feel better. ;)
TheWinkits wrote:They both looks of cuking amazing
User avatar
Chronos
[Most Active Member 2010]
 
Posts: 4457
Joined: Mon Aug 31, 2009 6:00 pm
Location: Californania

Re: For/While/Foreach

Postby Mystery » Sat Mar 27, 2010 1:39 pm

You support everything.
I on the other hand don't know what this is.
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: For/While/Foreach

Postby Dakta » Sat Mar 27, 2010 11:55 pm

Real if/elseif/esle statements would keep us from having to do this:

Code: Select all
Scene.my.checkWavelength = (Scene.my.wavelength >= 380.0) && (Scene.my.wavelength < 440.0) ? {
   Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
   Scene.my.green = 0.0;
   Scene.my.blue = 1.0;
} : {(Scene.my.wavelength >= 440.0) && (Scene.my.wavelength < 490.0) ? {
      Scene.my.red = 0.0;
      Scene.my.green = (Scene.my.wavelength - 440.0)/(490.0-440.0);
      Scene.my.blue = 1.0;
   } : {(Scene.my.wavelength >= 490.0) && (Scene.my.wavelength < 510.0) ? {
         Scene.my.red = 0.0;
         Scene.my.green = 0.0;
         Scene.my.blue = (Scene.my.wavelength - 510.0)/(510.0-490.0);
      } : {(Scene.my.wavelength >= 510.0) && (Scene.my.wavelength < 580.0) ? {
            Scene.my.red = (Scene.my.wavelength - 510.0)/(580.0-510.0);
            Scene.my.green = 1.0;
            Scene.my.blue = 0.0;
         } : {(Scene.my.wavelength >= 580.0) && (Scene.my.wavelength < 645.0) ? {
               Scene.my.red = 1.0;
               Scene.my.green = -(Scene.my.wavelength - 645.0)/(645.0-580.0);
               Scene.my.blue = 0.0;
            } : {(Scene.my.wavelength >= 645.0) && (Scene.my.wavelength <= 780.0) ? {
                  Scene.my.red = 1.0;
                  Scene.my.green = 0.0;
                  Scene.my.blue = 0.0;
               } : {
                     Scene.my.red = 0.0;
                     Scene.my.green = 0.0;
                     Scene.my.blue = 0.0;
                  }
               }
            }
         }
      }
   }


Where in most other languages you can do this (I'm using PHP as an example):
Code: Select all
function $checkWavelength(wavelength) {
   if ($wavelength >= 380.0 && $wavelength < 440.0) {
      $red = -($wavelength - 440.0)/(440.0-380.0);
      $green = 0.0;
      $blue = 1.0;
   } elseif ($wavelength >= 440.0 && $wavelength < 490.0) {
      $red = 0.0;
      $green = ($wavelength - 440.0)/(490.0-440.0);
      $blue = 1.0;
   } elseif ($wavelength >= 490.0 && $wavelength < 510.0) {
      $red = 0.0;
      $green = 0.0;
      $blue = ($wavelength - 510.0)/(510.0-490.0);
   } elseif ($wavelength >= 510.0 && $wavelength < 580.0) {
      $red = ($wavelength - 510.0)/(580.0-510.0);
      $green = 1.0;
      $blue = 0.0;
   } elseif ($wavelength >= 580.0 && $wavelength < 645.0) {
      $red = 1.0;
      $green = -($wavelength - 645.0)/(645.0-580.0);
      $blue = 0.0;
   } elseif ($wavelength >= 645.0 && $wavelength <= 780.0) {
      $red = 1.0;
      $green = 0.0;
      $blue = 0.0;
   } else {
      $red = 0.0;
      $green = 0.0;
      $blue = 0.0;
   }
}

Which is much easier to skim and write than über nested conditionals.


And writing stupid little bits of code to run through arrays and apply arguments to each item. Heck, I don't even know much about doing that since its such a pain in the ass. I have almost abandoned my simple modular transmission script because it is such a pain in the ass to deal with these sorts of things. I'm stuck with code that allows you to shift into nonexistent gears and not shift back. But this is a story for another thread.

Here's an example written in PHP. This example is part of some code I wrote that takes a nested array of the directory hierarchy and spits it out as a linked nested HTML unordered list for use in a navbar:
Code: Select all
foreach ($array as $item => $value) {
   if ( is_string ( $value ) ) { //if it's a file
      echo '<li class="page"><a href="'.$path.'/'.$value.'">'.$value.'</a></li>'; //create a list item and stuff
      echo "\n"; //put in a new line in the html so we can read it easier
   }
   elseif (is_array($value)) {
      $oldpath = $path.'';
      $path = $path.'/'.$directory.$item;
      echo '<li class="dir><a href="'.$path.'">'.$item.'</a><ul>';
      echo "\n"; //put in another line for ease of reading
      check_type($value,$path); //it's a directory, so call the function again
      echo '</ul></li>'; //close up the directory we made earlier
      echo $path;
      $path = $oldpath.''; //change the path back to what it was before
   }
}

Which is nice and simple to skim, write, and understand.


And what about while statements? We don't have 'em, do we? So we have to use a ternary conditional. Like so:
Code: Select all
Scene.my.monkey = (Scene.my.value = 108.0)  ? { //do some code here } : {}


Whereas it makes more sense to use a while statement like so (again in PHP):
Code: Select all
while ($value = 108.0) {
   //do something here
}




And why can't I find any information on creating for structures, because I know they exist in Thyme already! Grady's tutorial covers extending them past 50 loops, but not about creating them to begin with! WTF? Here's an example of one:
Code: Select all
for(//number of times to repeat the action , //action to take)
//example:
for(10, (x)=>{Scene.addCircle({circle stuff here})})

But this only works as a simple repeat function. It needs some improvement to be fully useful. Basically, a real for loop is like an advanced while loop. It does these four things:

1) Set a counter variable to some initial value.
2) Check to see if the conditional statement is true.
3) Execute the code within the loop.
4) Increment a counter at the end of each iteration through the loop.[

For example, in PHP:
Code: Select all
for ( initialize a counter; conditional statement; increment a counter){
   do this code;
}

for ($monkeys = 1 //we have one monkey; $monkeys <= 5 //until we have five monkeys; $monkeys += 1 //add a monkey every time we finish doing this){
   echo 'We have '.$monkeys.' monkeys!';
   echo '/n'; //new line
}

Which outputs this:
We have 1 monkeys!
We have 2 monkeys!
We have 3 monkeys!
We have 4 monkeys!
We have 5 monkeys!

Now is that something you'd like to be able to do or not?


And while we're at it, what about switch? (Takes a single input and checks it against any number of cases for values of that variable and executes code accordingly) For example (PHP):
Code: Select all
$destination = "Tokyo";
switch ($destination){
   case "Las Vegas":
      echo "Bring an extra $500";
      break;
   case "Amsterdam":
      echo "Bring an open mind";
      break;   
   case "Egypt":
      echo "Bring 15 bottles of SPF 50 Sunscreen";
      break;   
   case "Tokyo":
      echo "Bring lots of money";
      break;
   case "Caribbean Islands":
      echo "Bring a swimsuit";
      break;   
}

Which in this case will output:
Bring lots of money!
This saves a lot of hassle of creating a bulky conditional statement to check the variable (in this case $destination) against all sorts of possible values. Sounds like a good idea, no?


I hope this better illustrates what I meant in my first post. If you have any further questions, feel free to ask for clarification.

(I used PHP because it has fairly standard implementations of these statements, and it has all of them. I would have to have had to use different languages for each one :P)
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm


Return to Suggestions

Who is online

Users browsing this forum: No registered users and 3 guests