Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Does QB64pe already have a function like IFF ?
#1
This is something I want in BAM.  If QB64pe already has a similar function with a different name, I'd set up BAM's IFF function with the same name.


Code: (Select All)
_GetUrlKey$ = IFF( step2%, LEFT$(step1$, step2% - 1 ), step1$)
Reply
#2
What does an IFF do?
Reply
#3
(06-03-2023, 07:43 PM)SMcNeill Wrote: What does an IFF do?

I only know the IFF (Interchange File Format) introduced by "Electronic Arts" in 1985 https://github.com/1fish2/IFF, but that's obviously not what he thinks of according to his example.
Reply
#4
(06-03-2023, 07:43 PM)SMcNeill Wrote: What does an IFF do?

From this Wikipedia article:

Code: (Select All)
IIf(expr, truepart, falsepart)

Although the code-volume-savings is nothing to brag about at all, it is very nice cognitively (semantically?) for assigning values to variables (and maybe some other circumstances I'm not thinking of).

So putting the focus on a variable:

Code: (Select All)
variable = IIF( expr, truevalue, falsevalue )

instead of putting the focus on an IF THEN ELSE statement:

Code: (Select All)
IF expr THEN variable = truevalue ELSE variable = falsevalue
Reply
#5
If of interest.

IIF is like conditional (ternary) operators ...

... in C++:

Code: (Select All)
(condition) ? true-clause : false-clause

... in javascript:

Code: (Select All)
condition ? exprIfTrue : exprIfFalse
Reply
#6
I get by like this:

Code: (Select All)
dim i as _integer64, f as _float, x as _integer64, y as _float, s as string
i = iff(x > 5, x - 1, x * 2)
f = ifff(y < 0, 1, sqr(y))
s = iffs$(x = 0, "true", "false")
end

function iff&& (cond%%, truevalu&&, falsvalu&&)
    if cond%% then
        iff&& = truevalu&&
        exit function
    end if
    iff&& = falsvalu&&
end function

function ifff## (cond%%, truevalu##, falsvalu##)
    if cond%% then
        ifff## = truevalu##
        exit function
    end if
    ifff## = falsvalu##
end function

function iffs$ (cond%%, truevalu as string, falsvalu as string)
    if cond%% then
        iffs$ = truevalu
        exit function
    end if
    iffs$ = falsvalu
end function

It requires three functions, one for integers, another for floating point and another for strings, that's the main drawback. But be glad BASIC doesn't have a "boolean" type that would have prevented the straight-out use of a condition which is evaluated and passed along as integer value.
Reply
#7
(06-03-2023, 10:32 PM)mnrvovrfc Wrote: I get by like this:

Code: (Select All)
dim i as _integer64, f as _float, x as _integer64, y as _float, s as string
i = iff(x > 5, x - 1, x * 2)
f = ifff(y < 0, 1, sqr(y))
s = iffs$(x = 0, "true", "false")
end

function iff&& (cond%%, truevalu&&, falsvalu&&)
    if cond%% then
        iff&& = truevalu&&
        exit function
    end if
    iff&& = falsvalu&&
end function

function ifff## (cond%%, truevalu##, falsvalu##)
    if cond%% then
        ifff## = truevalu##
        exit function
    end if
    ifff## = falsvalu##
end function

function iffs$ (cond%%, truevalu as string, falsvalu as string)
    if cond%% then
        iffs$ = truevalu
        exit function
    end if
    iffs$ = falsvalu
end function

It requires three functions, one for integers, another for floating point and another for strings, that's the main drawback. But be glad BASIC doesn't have a "boolean" type that would have prevented the straight-out use of a condition which is evaluated and passed along as integer value.

BAM programs being interpreted by javascript (i.e. wwwbasic.js, significantly modified), it took me all of 10 minutes to just go ahead and setup an IFF function.

Although not very BASIC-like, (javascript not being strongly typed) the IFF function works as-is for both strings and whatever numbers (so I'm kind of appreciating the no fuss no muss compactness).

Your workaround is quite lovely, though.  That would make for an excellent include library.
Reply
#8
(06-03-2023, 10:59 PM)CharlieJV Wrote: BAM programs being interpreted by javascript (i.e. wwwbasic.js, significantly modified), it took me all of 10 minutes to just go ahead and setup an IFF function.

Although not very BASIC-like, (javascript not being strongly typed) the IFF function works as-is for both strings and whatever numbers (so I'm kind of appreciating the no fuss no muss compactness).

The Javascript version should work for any type including user-defined types, those that could have methods or not, but to assign values to the "properties". In QB64 one would have to write a new "IFF" function for each user-defined type, along the lines of my demonstration. Even if it involves a mere assignment such as "firstrecord = secondrecord".
Reply
#9
(06-05-2023, 02:54 PM)mnrvovrfc Wrote:
(06-03-2023, 10:59 PM)CharlieJV Wrote: BAM programs being interpreted by javascript (i.e. wwwbasic.js, significantly modified), it took me all of 10 minutes to just go ahead and setup an IFF function.

Although not very BASIC-like, (javascript not being strongly typed) the IFF function works as-is for both strings and whatever numbers (so I'm kind of appreciating the no fuss no muss compactness).

The Javascript version should work for any type including user-defined types, those that could have methods or not, but to assign values to the "properties". In QB64 one would have to write a new "IFF" function for each user-defined type, along the lines of my demonstration. Even if it involves a mere assignment such as "firstrecord = secondrecord".

Just to be clear, BAM, although allowing some things to not be strongly typed (numeric vs string), BAM only allows standard types.

This IFF in BAM will only work with primitive types (string, integer, floating point).

BAM programs do not allow embedded javascript.  Only BASIC things that are recognized by the interpreter.
Reply
#10
Hi Charlie what does BAM do here?
Code: (Select All)
Boolean = (7 < 8) ' this is a boolean expression NOT a string! that evaluates to 1 or -1
Print Boolean

Boolean = (7 > 8) '  this is a boolean expression NOT a string! that evaluates to 0
Print Boolean
I ask because IFF typically takes a numeric expression that evaluates to "True" or "False" numerically -1 or 1 and 0 for false.

mnrvovrfc had it setup correctly, I don't see where you are inferring a string expression for the first parameter.
b = b + ...
Reply




Users browsing this thread: 4 Guest(s)