Page 1 of 3

ternary if or iif

Posted: Tue Nov 10, 2009 9:50 am
by idle
I'm not sure if this is planned to be done though it could be very handy to have a ternary if or iif function
while it can be done to some extent with macro's the cost of the procedure call is a bit of a performance hit
and would be really nice to have in native code for convince.

see http://www.purebasic.fr/english/viewtop ... 12&t=39843

Code: Select all

Procedure.s IIFEvals(expr,*y,*n)
If expr
 ProcedureReturn PeekS(*y)
Else
 ProcedureReturn PeekS(*n)
EndIf
EndProcedure

Procedure IIFEvali(expr,y,n)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Procedure.f IIFEvalf(expr,y.f,n.f)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Procedure.d IIFEvald(expr,y.d,n.d)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Procedure IIFEvalq(expr,y.q,n.q)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Macro IIFI(expr,y,n)
IIFEvali((Not(expr)),n,y)
EndMacro

Macro IIFF(expr,y,n)
IIFEvalf((Not(expr)),n,y)
EndMacro

Macro IIFD(expr,y,n)
IIFEvald((Not(expr)),n,y)
EndMacro

Macro IIFQ(expr,y,n)
IIFEvalf((Not(expr)),n,y)
EndMacro

Macro IIFS(expr,y,n)
IIFEvals((Not(expr)),n,y)
EndMacro

Macro IIFfn(expr,a,b)
 If expr : a : Else : b : EndIf  
EndMacro 

Procedure p2(a.s)
  MessageRequester("test",a)
EndProcedure
  

a=5
b=4
c=2
d=3
e.f = 5.6
f.f = 5.5 

IIFfn(5>4,p2("hello"),MessageRequester("test","no"))
Debug IIFS(5.3>5.4,@"yes",@"no")
Debug IIFI(a > b,200,100) 
Debug IIFF(e > f,200.1234567,100.1234567)
Debug IIFD(5.5 = 5.6,200.123456789012345,10.123456789012345)
Debug IIFQ(a > c,$FFFFFFFF,$EEEEEEEE)


lp = 100000000
st = GetTickCount_()
For a = 1 To lp
   x = IIFi(5.5 > 7.5,200,100)
Next   
et1.f = (GetTickCount_() - st) / lp
st = GetTickCount_()
For a = 1 To lp
   If 5.5 > 7.5
      x = 200
   Else
      x = 100     
   EndIf
Next     
et2.f = (GetTickCount_() - st) / lp


MessageRequester("test", "IIFs " + StrF(et1,6) + " if " + StrF(et2,6))

Re: ternary if or iif

Posted: Tue Nov 10, 2009 4:12 pm
by luis
Would be nice to have inline procedures, this would solve a lot of similar problems (uhm, not problems really, requests). Not exactly as a native statement but very close without the activation record overhead.

In any case, I would like to have IIF me too. :)

Re: ternary if or iif

Posted: Tue Nov 10, 2009 4:29 pm
by Kaeru Gaman
I'm sorry to disturb here, but Fred already announced a Boolean() directive for a future version.
since that would change things completely, asking for some IIF with those examples seems a bit aside the roadmap...

Re: ternary if or iif

Posted: Tue Nov 10, 2009 5:53 pm
by Kale
idle wrote:I'm not sure if this is planned to be done though it could be very handy to have a ternary if or iif function
while it can be done to some extent with macro's the cost of the procedure call is a bit of a performance hit
and would be really nice to have in native code for convince.

see http://www.purebasic.fr/english/viewtop ... 12&t=39843

Code: Select all

...horrible duplication and in need of OOP...
What's wrong with:

Code: Select all

MyVar.s = IsTrue ? "true" : "false"

Re: ternary if or iif

Posted: Tue Nov 10, 2009 5:57 pm
by Kaeru Gaman
Kale wrote:What's wrong with:

Code: Select all

MyVar.s = IsTrue ? "true" : "false"
it's not wrong, but it's not BASIC, either.

Re: ternary if or iif

Posted: Tue Nov 10, 2009 6:52 pm
by Kale
Kaeru Gaman wrote:
Kale wrote:What's wrong with:

Code: Select all

MyVar.s = IsTrue ? "true" : "false"
it's not wrong, but it's not BASIC, either.
Exactly! But it's a lot more easy to read than that code up there. Does any BASIC support condition expressions?

Re: ternary if or iif

Posted: Tue Nov 10, 2009 7:21 pm
by Kaeru Gaman
most BASICs support conditional expressions / Boolean Expressions.
I know it since '83 from C64-Basic.

... or do you mean a IIF function?
that isn't BASIC at all.

Re: ternary if or iif

Posted: Tue Nov 10, 2009 8:10 pm
by Kale
Kaeru Gaman wrote:most BASICs support conditional expressions / Boolean Expressions.
I know it since '83 from C64-Basic.
Conditional expressions are not found in any BASIC language apart from VB which has the IIF() function which apparently isn't a true conditional expression but just a function.

http://en.wikipedia.org/wiki/IIf

I honestly don't think IIF is needed in PB because you can use a normal IF. Also i'm sure you could code a nicer way of implementing it using pointers? :?:

Re: ternary if or iif

Posted: Tue Nov 10, 2009 8:14 pm
by DoubleDutch
Who really cares? - most of PureBasic isn't anything like the original BASIC dialects.

iff +1

Re: ternary if or iif

Posted: Tue Nov 10, 2009 8:16 pm
by Kale
DoubleDutch wrote:Who really cares? - most of PureBasic isn't anything like the original BASIC dialects.

iff +1
True, but i'm just arguing if you're going to implement this, do it as an operator NOT a function and make it simple and easy to use. Operators can be optimised more to create inline code.

Code: Select all

MyVar.s = IsTrue ? "true" : "false"
This is far easier to read and use than the code above.

Re: ternary if or iif

Posted: Tue Nov 10, 2009 8:34 pm
by DoubleDutch
I agree with you that it should be an operator, not a function.

Re: ternary if or iif

Posted: Tue Nov 10, 2009 9:10 pm
by idle
Kale wrote:

Code: Select all

...horrible duplication and in need of OOP...
OOP wouldn't help at all, there's a reason for the repetition: Typecasting and Speed.
If you can do better then please do more than blow.

The reason it's posted is to show why we need / want a Ternary IF
It can't really be done efficiently with the overhead of a procedure call, while the resultant code works out around ~2x slower
than the normal ifs it's not that bad considering but it can only really be done efficiently if it's added to the language.

I prefer the function look to it x= iff(a > b, "yes","No")

Although it's not necessary, some people like ternary ifs, so they can use them with function calls or whatever.

If CreateFile(0, "MacroTest.txt")
CloseFile(0)
Debug "Delete File : " + IFFs(DeleteFile("MacroTest.txt"),@"Yes",@"No")
EndIf

Re: ternary if or iif

Posted: Tue Nov 10, 2009 9:27 pm
by Kale
idle wrote:OOP wouldn't help at all, there's a reason for the repetition
I'm sorry but there's no reason at all for repetition.

And OOP would help a great deal here, it's called Polymorphism, one of the pillars of OOP.

http://en.wikipedia.org/wiki/Polymorphi ... rogramming

Like i wrote earlier, im sure theres a better way to approach this using pointers, instead of the repitition.

Re: ternary if or iif

Posted: Tue Nov 10, 2009 10:09 pm
by idle
While I understand your sentiment, OOP isn't really going to help! It's not supposed to be easy on your eyes, it's supposed to be fast!

Re: ternary if or iif

Posted: Tue Nov 10, 2009 10:40 pm
by Foz
Ok, that's enough. The arguments of OOP vs Procedural will carry on into the eons and beyond. I'm fairly sure that the civil war in heaven was over OOP vs Procedural in the first place as well. And no, I don't know which side chose which. Though the "Generic Object Delay" does lend itself to the heavenly side... and in the furnace of Hell resides the "Scheme, Assemble, Test, Announce: Now" train of thought. ;)

I agree that an OOP "variant" type would be useful for an IIF() function, so that there is one base type that holds all, and then it can be casted as required.

As this isn't going to happen in PB anytime soon, the idea of using pointers instead is also of merit, but I think could leads itself to being unnecessarily complicated - it just depends on how it is implemented.

However, I think that a true ternary operator would be far easier to read and write : +1 from me :D