1. Short form of "if" statement

Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:

if x < 0 then x = -x

instead of

if x < 0
x = -x 
end if

I think, it would be very useful.

new topic     » topic index » view message » categorize

2. Re: Short form of "if" statement

SnakeCharmer said...

Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:

if x < 0 then x = -x

instead of

if x < 0
x = -x 
end if

I think, it would be very useful.

You could use iif...

include "std/utils.e" 
 
x = iif( x < 0, -x, x ) 

Of course, mind the warnings...

manual said...

Warning Note: You must take care when using this function because just like all other Euphoria routines, this does not do any lazy evaluation. All parameter expressions are evaluated before the function is called, thus, it cannot be used when one of the parameters could fail to evaluate correctly. For example, this is an improper use of the iif method

first = iif(sequence(var), var[1], var) 

The reason for this is that both var[1] and var will be evaluated. Therefore if var happens to be an atom, the var[1] statement will fail. In situations like this, it is better to use the long method.

if sequence(var) then 
    first = var[1] 
else 
    first = var 
end if 

Although, it looks like you're trying to get the absolute value of an atom, for which we already have an abs function:

include "std/math.e" 
 
x = abs({10.5, -12, 3}) 
-- x is {10.5, 12, 3} 
 
i = abs(-4) 
-- i is 4 

-Greg

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

3. Re: Short form of "if" statement

SnakeCharmer said...

Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:

if x < 0 then x = -x

instead of

if x < 0
x = -x 
end if

I think, it would be very useful.

Note that 'then' is required for the 'if' statement, so your second code example should be ...

if x < 0 then 
x = -x 
end if 

Also note that you do not have to use multiple lines when using the 'if - then - end if' form. eg...

if x < 0 then x = -x end if 

Which is very close to what your suggestion was about.

The reason that the syntax requires 'then', and 'end if' is to allow multiple statements to exist in an 'if' statement and to not rely on text (line) formatting as a syntax requirement.

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

4. Re: Short form of "if" statement

ghaberek said...

Although, it looks like you're trying to get the absolute value of an atom, for which we already have an abs function:

include "std/math.e" 
 
x = abs({10.5, -12, 3}) 
-- x is {10.5, 12, 3} 
 
i = abs(-4) 
-- i is 4 

-Greg

I think this is the right way to do it.

ghaberek said...
SnakeCharmer said...

Offer to include "then" keyword for "if" with only consequence and without "else" branch. Example:

if x < 0 then x = -x

instead of

if x < 0
x = -x 
end if

I think, it would be very useful.

You could use iif...

include "std/utils.e" 
 
x = iif( x < 0, -x, x ) 

Of course, mind the warnings...

manual said...

Warning Note: You must take care when using this function because just like all other Euphoria routines, this does not do any lazy evaluation. All parameter expressions are evaluated before the function is called, thus, it cannot be used when one of the parameters could fail to evaluate correctly. For example, this is an improper use of the iif method

first = iif(sequence(var), var[1], var) 

The reason for this is that both var[1] and var will be evaluated. Therefore if var happens to be an atom, the var[1] statement will fail. In situations like this, it is better to use the long method.

if sequence(var) then 
    first = var[1] 
else 
    first = var 
end if 

One could use the iif preprocessor to add support for lazy evaluation: http://scm.openeuphoria.org/hg/euphoria/rev/cf8a5675c514

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

5. Re: Short form of "if" statement

DerekParnell said...

Also note that you do not have to use multiple lines when using the 'if - then - end if' form. eg...

if x < 0 then x = -x end if 

Which is very close to what your suggestion was about.

Yeah, you are right. Two BASIC dialects were mixed in my brain. Excuse for troubling.

P.S. I am going to refuse the second dialect - PureBasic. Euphoria is able everything that I need. The euphoric feeling better than purity!

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

Search



Quick Links

User menu

Not signed in.

Misc Menu