jdBasic lets you operate on whole arrays (vectors, matrices, N-D arrays) without writing slow loops. This page is a practical guide with copy/paste examples.
Standard operators apply to every element (broadcasting scalars).
V = [10, 20, 30, 40]
PRINT "V * 2: "; V * 2
PRINT "V + 100: "; V + 100
(From the official array guide.) See also: Manual
If shapes match, operations happen element-by-element.
A = [1, 2, 3]
B = [10, 20, 30]
PRINT "A + B: "; A + B
IOTA(N) generates a 1D array from 1..N (perfect for indexing and simulation).
R = IOTA(10)
PRINT R
Pour data into a new shape. Use FRMV$ to print matrices nicely.
flat = IOTA(12)
M = RESHAPE(flat, [3, 4])
PRINT FRMV$(M)
Apply a function to every element (map). The lambda syntax makes one-liners possible.
print SELECT(lambda i -> i + 1, iota(10))
Chain array transformations with a pipe. (The ? passes the previous result.)
print SELECT(lambda i -> i + 1, iota(10)) |> FILTER(lambda val -> val > 5, ?) |> SELECT(lambda v -> v * 10, ?)
If you prefer explicit functions, jdBasic also supports passing function references (with @).
FUNC SQUARE_IT(X)
RETURN X * X
ENDFUNC
result = SELECT(SQUARE_IT@, [1, 2, 3, 4])
PRINT result
V = IOTA(8)
PRINT "TAKE(3, V) = "; TAKE(3, V)
V = IOTA(8)
PRINT "DROP(3, V) = "; DROP(3, V)
M = [[1,2],[3,4]]
PRINT "Transpose of M: "; TRANSPOSE(M)
V = [10, 20, 30]
PRINT "Reverse of V: "; REVERSE(V)
Rotate clockwise by transposing and reversing (used in your Tetris example too).
FUNC RotateClockwise(mat)
RETURN REVERSE(TRANSPOSE(mat))
ENDFUNC
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
C = MATMUL(A, B)
PRINT "MATMUL(A, B) ="; C
Create tables by applying an operator/function to every pair of values.
V1 = [10, 20, 30]
V2 = [1, 2]
mult_table = OUTER(V1, V2, "*")
PRINT mult_table
This approach builds a set of odd candidates and subtracts composites using DIFF.
PRINT "--- Calculating Primes using Set Difference ---"
LIMIT = 1000
' 1) Potential primes: odd numbers from 3..LIMIT
ODDS = (IOTA(LIMIT/2) - 1) * 2 + 3
' 2) Potential odd factors up to sqrt(LIMIT)
FACTORS = []
FOR I = 3 TO SQR(LIMIT) STEP 2
FACTORS = APPEND(FACTORS, I)
NEXT I
' 3) Generate odd composites
COMPOSITES = []
FOR I = 0 TO LEN(FACTORS)-1
P = FACTORS[I]
FOR J = P * P TO LIMIT STEP P * 2
COMPOSITES = APPEND(COMPOSITES, J)
NEXT J
NEXT I
' 4) Set difference -> odd primes
ODD_PRIMES = DIFF(ODDS, COMPOSITES)
' 5) Final prime list
ALL_PRIMES = APPEND([2], ODD_PRIMES)
PRINT ALL_PRIMES
Based on your provided apl_prime.jdb and the manual’s APL-style prime sieve section.
W=40:H=20:F=6:C=RESHAPE([" "],[H,W]):X=IOTA(W)-1:Y=INT((SIN(X/F)+1)*((H-1)/2)):C[Y,X]="*":C[H/2,IOTA(W)-1] = "-":PRINT FRMV$(C)
BD="1967-08-18":W=41:H=21:D=DATEDIFF("D",CVDATE(BD),NOW()):X=D-W/2+IOTA(W):C=RESHAPE([" "],[H,W]):PY=INT((SIN(2*PI*X/23)+1)*((H-1)/2)):EY=INT((SIN(2*PI*X/28)+1)*((H-1)/2)):IY=INT((SIN(2*PI*X/33)+1)*((H-1)/2)):C[H/2,IOTA(W)-1]="-":C[IOTA(H)-1,INT(W/2)]="|":C[PY,IOTA(W)-1]="P":C[EY,IOTA(W)-1]="E":C[IY,IOTA(W)-1]="I":PRINT "Biorhythm for " + BD + " | P=Physical(23) E=Emotional(28) I=Intellectual(33)":PRINT FRMV$(C)
' Read a text file into an array (split by newline)
T = SPLIT(TXTREADER$("Filename.txt"), VBNEWLINE)
' Write an array back to a text file (one line per element)
TXTWRITER "Filename.txt", FRMV$(STACK(1, T, RESHAPE([VBNEWLINE], LEN(T))), "{:<}")