jdBasic Try Online
Element-wise math Reshape + matrices Take / Drop Transpose / Reverse Outer products Set operations One-liners

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.

Best next steps

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 scalars).

Copy
V = [10, 20, 30, 40]
PRINT "V * 2: "; V * 2
PRINT "V + 100: "; V + 100

(From the official array guide.) See also: Manual

Array + array

If shapes match, operations happen element-by-element.

Copy
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 (perfect for indexing and simulation).

Copy
R = IOTA(10)
PRINT R

RESHAPE: turn vectors into matrices

Pour data into a new shape. Use FRMV$ to print matrices nicely.

Copy
flat = IOTA(12)
M = RESHAPE(flat, [3, 4])
PRINT FRMV$(M)

Functional style: SELECT, FILTER, pipelines

SELECT with lambdas

Apply a function to every element (map). The lambda syntax makes one-liners possible.

Copy
print SELECT(lambda i -> i + 1, iota(10))

Pipeline: SELECT → FILTER → SELECT

Chain array transformations with a pipe. (The ? passes the previous result.)

Copy
print SELECT(lambda i -> i + 1, iota(10)) |> FILTER(lambda val -> val > 5, ?) |> SELECT(lambda v -> v * 10, ?)

Higher-order functions (classic style)

If you prefer explicit functions, jdBasic also supports passing function references (with @).

Copy
FUNC SQUARE_IT(X)
  RETURN X * X
ENDFUNC

result = SELECT(SQUARE_IT@, [1, 2, 3, 4])
PRINT result

Take and Drop

TAKE: first N items

Copy
V = IOTA(8)
PRINT "TAKE(3, V) = "; TAKE(3, V)

DROP: remove first N items

Copy
V = IOTA(8)
PRINT "DROP(3, V) = "; DROP(3, V)

Matrix transforms: TRANSPOSE and REVERSE

TRANSPOSE (flip across diagonal)

Copy
M = [[1,2],[3,4]]
PRINT "Transpose of M: "; TRANSPOSE(M)

REVERSE (reverse vector order)

Copy
V = [10, 20, 30]
PRINT "Reverse of V: "; REVERSE(V)

Rotate a 2D matrix (classic trick)

Rotate clockwise by transposing and reversing (used in your Tetris example too).

Copy
FUNC RotateClockwise(mat)
  RETURN REVERSE(TRANSPOSE(mat))
ENDFUNC

Linear algebra: MATMUL and OUTER

MATMUL (matrix multiplication)

Copy
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
C = MATMUL(A, B)
PRINT "MATMUL(A, B) ="; C

OUTER (build a table fast)

Create tables by applying an operator/function to every pair of values.

Copy
V1 = [10, 20, 30]
V2 = [1, 2]
mult_table = OUTER(V1, V2, "*")
PRINT mult_table

Set theory: DIFF + prime sieve

Primes using set difference (APL style)

This approach builds a set of odd candidates and subtracts composites using DIFF.

Copy
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.

One-liners gallery

ASCII sine wave plot (single line)

Copy
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 (single line)

Copy
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 file → array, write array → file

Copy
' Read a text file into an array (split by newline)
T = SPLIT(TXTREADER$("Filename.txt"), VBNEWLINE)
Copy
' Write an array back to a text file (one line per element)
TXTWRITER "Filename.txt", FRMV$(STACK(1, T, RESHAPE([VBNEWLINE], LEN(T))), "{:<}")

Next steps

More examples

Small, practical snippets.

Basic examples →

Arrays meet AI

Tensors + broadcasting + autodiff.

AI with BASIC →

Compare with Python

Where jdBasic shines.

jdBasic vs Python →