ternary if or iif

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: ternary if or iif

Post by Foz »

luis wrote:...make simple (or complicated) programs '60 style...

Anyone can be happy.
And anyone can be a hippy :mrgreen:
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: ternary if or iif

Post 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 ! :)
"Have you tried turning it off and on again ?"
A little PureBasic review
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: ternary if or iif

Post 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
Image
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Re: ternary if or iif

Post 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...
Ta - N
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: ternary if or iif

Post 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:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: ternary if or iif

Post 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)) 

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: ternary if or iif

Post 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.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: ternary if or iif

Post 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")
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: ternary if or iif

Post 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));
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: ternary if or iif

Post 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.
Last edited by Mistrel on Mon Nov 24, 2014 5:10 am, edited 2 times in total.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: ternary if or iif

Post 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
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: ternary if or iif

Post 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:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply