QBJS - Fun Facts!
#1
Starting a thread here to post fun facts about QBJS that you might not know.

Fun Fact #1 - You can define optional parameters!
Code: (Select All)
PrintLines "First Test"
PrintLines "Second Test", 4
PrintLines "Third Test", , " -> "

Sub PrintLines (msg As String, lines As Integer, prefix As String)
    If lines = undefined  Then lines = 2   ' default to 2 lines
    If prefix = undefined Then prefix = "" ' default to blank prefix
   
    Dim i As Integer
    For i = 1 To lines
        Print prefix; msg
    Next i
End Sub

Output:
Code: (Select All)
First Test
First Test
Second Test
Second Test
Second Test
Second Test
-> Third Test
-> Third Test

Try it out on QBJS
Reply
#2
   
Reply
#3
Say, this is a pretty enlightening demo dbox! In case people missed it, the demo implies undefined is a reserved word in qbjs. Power overwhelming!
Reply
#4
STRING$() function has weird behavior in QBJS. Is this on purpose?

Because I tried to test with the following code:

Code: (Select All)
print string$(80, 42)
print string2$(80, 42)
print string$(40, "*+-")
print string2$(40, "*+-")
print string2$(40, "*+-", 2)
print string2$(20, "*+-", 3)
end

function string2$(quant as integer, astr as string, numchar as integer)
    dim i as integer, sret$
    if astr = undefined then astr = chr$(32)
    if numchar = undefined then numchar = 1
    for i = 1 to quant
        sret$ = sret$ + left$(astr, numchar)
    next
    string2$ = sret$
end function

For the first line, I got "42" replicated 40 times. In QuickBASIC and "clones" it gives me 80 asterisks.

Even weirder, I do:

Code: (Select All)
print string$(10, 432)

and the result is "4324324324". Not sure but it's supposed to flag an error that the second parameter must be type byte.

Otherwise this is pretty neat and I could finally shelf my REPEAT$(). LOL.
Reply
#5
(04-11-2023, 06:11 AM)mnrvovrfc Wrote: STRING$() function has weird behavior in QBJS. Is this on purpose?

Good catch! Looks like I missed the fact that the STRING$ function can take either a string or a character code.  Fix will be included in the next release.
Reply
#6
Fun Fact #2 - Custom types can be returned from functions!

Code: (Select All)
Screen 12

Type Circ
    x As Integer
    y As Integer
    r As Integer
    c As Integer
End Type

Dim i As Integer
Dim c As Circ
Do
    Cls
    For i = 1 To 100
        c = MakeCircle
        Circle (c.x, c.y), c.r, c.c
    Next i
    _Limit 10
Loop

Function MakeCircle
    Dim c As Circ
    c.x = Rnd * 640
    c.y = Rnd * 480
    c.r = Rnd * 100 + 10
    c.c = Rnd * 14 + 1
    MakeCircle = c
End Function

Try it on QBJS
Reply
#7
you can write things without brackets ie

Code: (Select All)
cmul q, q, g
cdiv p, p, q


sub cmul( w(), z1(), z2() )
    x1 = z1(0)
    y1 = z1(1)
    a1 = z2(0)
    b1 = z2(1)

    w(0) = x1*a1 - y1*b1
    w(1) = x1*b1 + y1*a1
end sub

sub cdiv( w(), z1(), z2() )
    x1 = z1(0)
    y1 = z1(1)
    a1 = z2(0)
    b1 = z2(1)

    d1 = a1*a1 + b1*b1
    w(0) = (x1*a1 + y1*b1)/d1
    w(1) = (y1*a1 - x1*b1)/d1
end sub
Reply
#8
Fun Fact #3 - QBJS has native support for associative arrays (dictionary).

This feature is sometimes also known as a map or hashmap.

Code: (Select All)
Dim colors() As _Unsigned Long

colors("brick red") = &HFFC62D42
colors("electric lime") = &HFFCCFF00
colors("metalic sunburst") = &HFF9C7C38

Cls , 15
Line (10, 10)-(100, 100), colors("electric lime"), BF
Line (200, 200)-(300, 300), colors("brick red"), BF
Circle (200, 100), 50, colors("metalic sunburst")

Try it on QBJS
Reply
#9
Ah so that's how dictionary's are supposed to be used. I was wondering how I might apply the dictionary code I was working on yesterday. So I need to rename the Function that does a Value Lookup from a Key to something short and easy and maybe do a value conversion for numbers. Thanks for new ideas!

Now I wonder, could I do a whole program with variables and values from a dictionary? Bet I can! Be fun to try anyway Smile

Ha! The JS part is really showing up the QB part ;-))
b = b + ...
Reply
#10
(04-13-2023, 11:48 AM)dbox Wrote:
Code: (Select All)
Dim colors() As _Unsigned Long

colors("brick red") = &HFFC62D42
colors("electric lime") = &HFFCCFF00
colors("metalic sunburst") = &HFF9C7C38

Cls , 15
Line (10, 10)-(100, 100), colors("electric lime"), BF
Line (200, 200)-(300, 300), colors("brick red"), BF
Circle (200, 100), 50, colors("metalic sunburst")

This could be faked as function, and on 64-bit it's faster than any "dictionary" implementation:

Code: (Select All)
FUNCTION colors~& (mycolor$)
    DIM iret AS _UNSIGNED LONG
    IF mycolor$ = "brick red" THEN
        iret = &HFFC62D42
    ELSEIF mycolor$ = "electric lime" THEN
        iret = &HFFCCFF00
    ELSEIF mycolor$ = "metallic sunburst" THEN
        iret = &HFF9C7C38
    ELSE
        iret = 0
    END IF
    colors~& = iret
END FUNCTION
Reply




Users browsing this thread: 2 Guest(s)