Element-wise math
Reshape + matrices
One-liners
Set operations
Advanced Array Programming
APL-inspired power in jdBasic
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.
On this page
Resources
- New? Start here: Learn BASIC
- Need snippets? Basic Examples
- Arrays meet AI? AI with BASIC
Tip: If you're coming from Python, think “NumPy built-in” — but with classic BASIC syntax.
Core principle: operators work element-wise
Array + scalar
Standard operators apply to every element (broadcasting).
V = [10, 20, 30, 40]
PRINT "V * 2: "; V * 2
PRINT "V + 100: "; V + 100
Array + array
If shapes match, operations happen element-by-element.
A = [1, 2, 3]
B = [10, 20, 30]
PRINT "A + B: "; A + B
Create and shape arrays
IOTA: make ranges fast
IOTA(N) generates a 1D array from 1..N.
R = IOTA(10)
PRINT R
RESHAPE: vectors into matrices
Pour data into a new shape. Use FRMV$ to print nicely.
flat = IOTA(12)
M = RESHAPE(flat, [3, 4])
PRINT FRMV$(M)
Functional style: Pipelines
SELECT with lambdas
Apply a function to every element (map).
print SELECT(lambda i -> i + 1, iota(10))
Pipeline Operator
Chain transformations with a pipe |>.
print SELECT(lambda i -> i + 1, iota(10)) |> FILTER(lambda val -> val > 5, ?)
Matrix Transforms
TRANSPOSE
Flip across the diagonal.
M = [[1,2],[3,4]]
PRINT "Transpose of M: "; TRANSPOSE(M)
MATMUL
Standard matrix multiplication.
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
C = MATMUL(A, B)
PRINT C
Set Theory: Prime Sieve
Primes using Set Difference
This approach builds a set of odd candidates and subtracts composites using DIFF.
This is a classic APL/Array programming technique.
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
One-Liners Gallery
ASCII Sine Wave
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)
Biorhythm Chart
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)