Examples
Stream-First Vector Operations
# Create vectors
[ 1 2 3 4 5 ]
[ [ 1 2 ] [ 3 4 ] ]
[ 1 'hello' TRUE ]
# Access elements
[ 10 20 30 ] [ 0 ] GET # Result: [ 10 ]
[ 10 20 30 ] [ -1 ] GET # Result: [ 30 ]
# Modify
[ 1 2 3 ] [ 1 ] [ 99 ] INSERT # Result: [ 1 99 2 3 ]
[ 1 2 3 ] [ 1 ] [ 99 ] REPLACE # Result: [ 1 99 3 ]
[ 1 2 3 ] [ 1 ] REMOVE # Result: [ 1 3 ]
# Arithmetic with broadcasting
[ 1 2 3 ] [ 10 ] + # Result: [ 11 12 13 ]
[ [ 1 2 ] [ 3 4 ] ] [ 10 20 ] + # Result: [ [ 11 22 ] [ 13 24 ] ]
Defining Words
# Double a number
{ [ 2 ] * } 'DOUBLE' DEF
[ 5 ] DOUBLE # Result: [ 10 ]
# Square
{ DUP * } 'SQUARE' DEF
[ 4 ] SQUARE # Result: [ 16 ]
# Even check
{ [ 2 ] MOD [ 0 ] = } 'EVEN?' DEF
[ 6 ] EVEN? # Result: [ TRUE ]
Higher-Order Functions
# MAP
[ 1 2 3 4 5 ] 'DOUBLE' MAP # Result: [ 2 4 6 8 10 ]
# FILTER
[ 1 2 3 4 5 6 ] 'EVEN?' FILTER # Result: [ 2 4 6 ]
# FOLD
[ 1 2 3 4 5 ] [ 0 ] '+' FOLD # Result: [ 15 ]
[ 1 2 3 4 5 ] [ 1 ] '*' FOLD # Result: [ 120 ]
# Chaining
[ 1 2 3 4 5 6 7 8 9 10 ]
'EVEN?' FILTER # [ 2 4 6 8 10 ]
'SQUARE' MAP # [ 4 16 36 64 100 ]
[ 0 ] '+' FOLD # [ 220 ]
Conditional Branching with COND
[ -5 ]
{ [ 0 ] < } { 'negative' }
{ [ 0 ] = } { 'zero' }
{ IDLE } { 'positive' }
COND
Fraction Arithmetic
# Exact fractions
[ 1/3 ] [ 1/6 ] + # Result: [ 1/2 ]
[ 1/3 ] [ 2 ] * # Result: [ 2/3 ]
# No rounding errors
[ 0.1 ] [ 0.2 ] + # Result: [ 3/10 ]
# Large numbers
[ 1000000000000 ] [ 1000000000000 ] *
Matrix Operations
# Create matrix
[ 1 ] [ 9 ] RANGE [ 3 3 ] RESHAPE
# Result: [ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ]
# Transpose
[ [ 1 2 3 ] [ 4 5 6 ] ] TRANSPOSE
# Result: [ [ 1 4 ] [ 2 5 ] [ 3 6 ] ]
# Row sums
[ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ]
'[ 0 ] '+' FOLD' MAP # Result: [ [ 6 ] [ 15 ] [ 24 ] ]
# Fill with zeros
[ 3 3 ] [ 0 ] FILL
# Result: [ [ 0 0 0 ] [ 0 0 0 ] [ 0 0 0 ] ]
String Processing
# Reverse string
[ 'hello' ] CHARS REVERSE JOIN # Result: [ 'olleh' ]
# String length
[ 'hello' ] CHARS LENGTH # Result: [ 5 ]
# Palindrome check
{ CHARS DUP REVERSE = } 'PALINDROME?' DEF
[ 'radar' ] PALINDROME? # Result: [ TRUE ]
Date/Time
# Current time
NOW # Result: [ @2024-11-25 14:00:00 ]
# Extract components
NOW 'LOCAL' DATETIME # Result: [ [ 2024 11 25 14 0 0 ] ]
# Add one hour
NOW [ 3600 ] + # Adds 3600 seconds
# Create specific date
[ [ 2024 12 25 0 0 0 ] ] 'LOCAL' TIMESTAMP
Stack Mode
# Stack length
a b c d e .. LENGTH # Result: a b c d e [ 5 ]
# Get from stack
a b c d [ 2 ] .. GET # Result: a b c d [ c ]
# Reverse stack
a b c .. REVERSE # Result: c b a
# Combine to vector
a b c d e .. CONCAT # Result: [ a b c d e ]
Practical: Factorial
# Using FOLD
[ 1 ] [ 5 ] RANGE [ 1 ] '*' FOLD # 5! = [ 120 ]