Re: Short form of "if" statement

new topic     » goto parent     » topic index » view thread      » older message » newer message
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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu