Page 3 of 3

Re: ternary if or iif

Posted: Sat Nov 14, 2009 10:12 pm
by Foz
luis wrote:...make simple (or complicated) programs '60 style...

Anyone can be happy.
And anyone can be a hippy :mrgreen:

Re: ternary if or iif

Posted: Sat Nov 14, 2009 10:17 pm
by luis
Foz wrote:
luis wrote:...make simple (or complicated) programs '60 style...

Anyone can be happy.
And anyone can be a hippy :mrgreen:
I asked for it ! :)

Re: ternary if or iif

Posted: Sat Nov 14, 2009 10:24 pm
by moogle
luis wrote:Anyone can be happy.
Nahh they will never be happy because they want to stop everyone else getting these nice newer features because "it's not BASIC" or because it's C/C++/C# syntax :P

Re: ternary if or iif

Posted: Sun Nov 15, 2009 2:42 am
by naw
Kaeru Gaman wrote:this is C syntax.
if you like it that much, you can program in C (++/#)
there are tons of compilers out there for C
Well, maybe, maybe not (who cares?).
I'm not going to choose a language based solely on wether it uses ";" or ":" to separate commands or supports the use of "&&" or "||"
I'm not a pureist (no pun intended) who cares where these ideas come from?

The great thing about BASIC (any version) is that it isnt a *proud* language, it's standards were ignored/forgotten long ago - the point is, does "&&" and "||" add anything of value to the language or not?

If this syntax is useful, use it. If not, forget it.

Anyway, these are not my decisions to make (Fred / Freak?) - all I'm saying is I would like this syntax in PB, cos I think its useful...

Re: ternary if or iif

Posted: Sun Nov 23, 2014 2:39 pm
by Thunder93
I know I'm posting in old topic, but I'm posting on this and avoid duplicate topics in 'Feature Requests and Wishlists' forum.

I don't know how I've missed this discussions way back in 2009.

Before ever programming, I use to-do lot of scripting with different languages. It has been so long now but waayyy back in my teens .. I use to-do lot of scripting and the ternary conditional / inline IF (IIF) was a wonder to use. I personally found this to make things much easier and also keeps code looking optimized and pretty.

Idle, has offered code which is all nice and dandy. Like idle said though, without it being natively supported, you have ~2x speed penalty. Working with different types of projects, .. like networking projects. Speed is of top-most importance.

When I bought PureBasic, waayyy back in 2005. Then shortly later I was looking to use ternary conditional / inline IF (IIF). However, I couldn't spot such a feature and at the time I've summed it up to not being familiar with PureBasic commandset and simply need more time to become familiar and locate this command.


I'm more familiar with PureBasic now.. I don't believe that this support exists. LOL


Unfortunately all this time has passed, and very old requests been made, and still no inclusion. This support doesn't seem like it'll ever catch the interest of those that matters. :cry:


New days..., might bring surprises! :mrgreen:

Re: ternary if or iif

Posted: Sun Nov 23, 2014 8:33 pm
by idle
Funny reading the old thread and now I wonder what all the fuss was about and why the solution was so protracted.

Code: Select all

Macro iif(clause,condTrue,condFalse) 
  If Bool(clause) : condTrue : Else : condfalse : EndIf 
EndMacro   

Procedure foo(x,y) 
  Debug Str(x) + Str(y)
EndProcedure   
  
x=4:y=2

iif(x > 5 And y > 0, Debug "true", foo(x,y)) 


Re: ternary if or iif

Posted: Sun Nov 23, 2014 8:48 pm
by Thunder93
that isn't quite the same. IIF() you produced, can't exactly be in-line? Like to set a returned value into variable.

Re: ternary if or iif

Posted: Sun Nov 23, 2014 11:07 pm
by skywalk
Yeah, IIF()'s have to be Procedures and type specific for inline...

Code: Select all

Procedure$ IIFs(expression, IsTrue$, IsFalse$)
  If expression
    ProcedureReturn IsTrue$
  Else
    ProcedureReturn IsFalse$
  EndIf
EndProcedure
Procedure.i IIF(expression.i, IsTrue.i, IsFalse.i)
  If expression
    ProcedureReturn IsTrue
  Else
    ProcedureReturn IsFalse
  EndIf
EndProcedure

Procedure.i DoIt(a.i=1, b.i=2, x$=#NULL$)
  Debug a
  Debug b
  Debug x$
EndProcedure

DoIt(1, 2, IIFs(Bool("1" > "2"), "TRUE", "FALSE"))

DoIt(1, IIF(Bool("1" > "2"), 1, -999), "Using b")

Re: ternary if or iif

Posted: Sun Nov 23, 2014 11:54 pm
by Thunder93
Nice stuff. Some type of PB option exists to the counterpart.

I'm still looking for more convenience and freedom. The fact that there's no native support, we suffer ~2x lowered speeds. Working with certain types of projects especially networking, this is a big deal. That being the case, doing bulky and time-consuming If : elseIf : else expressions seems like a requirement.


Code: Select all

               reset->tcp.AckNum =
                    (tcp_header->Syn?
                        htonl(ntohl(tcp_header->SeqNum) + 1):
                        htonl(ntohl(tcp_header->SeqNum) + payload_len));

Re: ternary if or iif

Posted: Mon Nov 24, 2014 1:45 am
by Mistrel
Unless I'm missing something, you can already do this with the advent of Bool():

Code: Select all

Procedure _IIF_Integer(Expression, IsTrue, IsFalse)
  If Expression
    ProcedureReturn IsTrue
  EndIf
  
  ProcedureReturn IsFalse
EndProcedure

Macro IIFi(Expression, IsTrue, IsFalse)
  _IIF_Integer(Bool(Expression),IsTrue,IsFalse)
EndMacro

Result=IIFi(3>2,1,0)

Debug Result
The only downside is that we still need a separate function for each type, as we don't have anything like templates or generics.

Re: ternary if or iif

Posted: Mon Nov 24, 2014 5:02 am
by Danilo
Thunder93 wrote:The fact that there's no native support, we suffer ~2x lowered speeds. Working with certain types of projects especially networking, this is a big deal. That being the case, doing bulky and time-consuming If : elseIf : else expressions seems like a requirement.

Code: Select all

               reset->tcp.AckNum =
                    (tcp_header->Syn?
                        htonl(ntohl(tcp_header->SeqNum) + 1):
                        htonl(ntohl(tcp_header->SeqNum) + payload_len));
How many million/billion of this ternary if statements do you execute per second?

Code: Select all

If Bool(*tcp_header\Syn) : *reset\tcp\AckNum = htonl(ntohl(*tcp_header\SeqNum) + 1)
Else                     : *reset\tcp\AckNum = htonl(ntohl(*tcp_header\SeqNum) + payload_len)
EndIf
Your 2 times lowered speed (compared to what?) is in the microseconds range for one such call, and compared
to highly optimizing C++ compilers you are probably little bit slower with PB anyway.
Both languages have to test the expression at runtime, if it isn't a constant expression that could be optimized away.


I would include the result in the macro to make things easier to write. Of course it's not "Inline If" anymore, nonetheless it makes things easier to write:

Code: Select all

Macro IIF(_target_, _expression_, _true_, _false_)
    If Bool(_expression_) : _target_ = _true_ : Else : _target_ = _false_ : EndIf
EndMacro

Macro IIFcmd(_cmd_, _expression_, _true_, _false_)
    If Bool(_expression_) : _cmd_ _true_ : Else : _cmd_ _false_ : EndIf
EndMacro

Macro NONE : : EndMacro



IIF(Result, 3 > 2, 1, 0) : Debug Result

IIF(a$, x = 0, "True", "False") : Debug a$

IIF( d.d,   ; result/target
     1 = 2, ; test expression
     1.5,   ; true
     0.5 )  ; false

Debug d



IIFcmd( Debug, 1 = 1, "true", "false" )
IIFcmd( var$=, 1 > 1, "true", "false" )  : Debug var$



Procedure Func(text.s)
    Debug text
EndProcedure

IIFcmd( NONE,
        5 < 6,
        Func("5 <  6"),
        Func("5 >= 6") )



CompilerIf Defined(Thunder93, #PB_Constant)

    IIFcmd( *reset\tcp\AckNum =,
            *tcp_header\Syn,
            htonl(ntohl(*tcp_header\SeqNum) + 1),
            htonl(ntohl(*tcp_header\SeqNum) + payload_len) )

CompilerEndIf

Re: ternary if or iif

Posted: Mon Nov 24, 2014 10:24 pm
by Thunder93
Thank you.., you guys! For both information and example codes.

I do like the idea of using Macros.

Like you've said, this makes things easier to write. :mrgreen: