Prelude Functions¶
Overview¶
The JSL prelude provides the computational foundation for all JSL programs. These built-in functions are available in every JSL environment.
For a guide to creating and managing execution contexts, see the Environments documentation.
Special Forms (Core Syntax)¶
While the prelude contains a library of standard functions, the core language is defined by a small set of special forms. These are syntactic constructs that do not follow the standard evaluation rule (i.e., they don't necessarily evaluate all of their arguments).
The core special forms include if, def, lambda, do, let, and try. For a complete reference, please see the dedicated Special Forms documentation.
Design Principles¶
- Immutable Operations: Functions return new values rather than modifying inputs
- N-arity Support: Mathematical and logical operations accept variable numbers of arguments
- Type Safety: Comprehensive type predicates and safe conversions
- Functional Composition: Higher-order functions that work seamlessly with JSL closures
- JSON Compatibility: All operations respect JSON's type system
Data Constructors¶
list¶
Creates a list from the provided arguments.
List Operations¶
JSL provides comprehensive list manipulation functions following functional programming principles.
append¶
Returns a new list with the item appended to the end.
prepend¶
Returns a new list with the item prepended to the beginning.
concat¶
["concat", [1, 2], [3, 4], [5]] // → [1, 2, 3, 4, 5]
["concat", [1], [2]] // → [1, 2]
["concat"] // → []
first¶
Returns the first element of a list, or null if empty.
rest¶
Returns all elements except the first, or empty list if insufficient elements.
nth¶
Returns the element at the specified index (0-based), or null if out of bounds.
length¶
Returns the length of a list or string.
empty?¶
["empty?", []] // → true
["empty?", [1]] // → false
["empty?", ""] // → true
["empty?", "hi"] // → false
slice¶
["slice", [1, 2, 3, 4, 5], 1, 4] // → [2, 3, 4]
["slice", [1, 2, 3], 1] // → [2, 3]
["slice", "hello", 1, 4] // → "ell"
reverse¶
Returns a reversed copy of the list or string.
contains?¶
["contains?", [1, 2, 3], 2] // → true
["contains?", [1, 2, 3], 4] // → false
["contains?", "hello", "ell"] // → true
index¶
Returns the index of the first occurrence of item, or -1 if not found.
Dictionary Operations¶
Immutable dictionary operations supporting functional programming patterns.
get¶
["get", {"name": "Alice", "age": 30}, "name"] // → "Alice"
["get", {"name": "Alice"}, "age", "unknown"] // → "unknown"
set¶
["set", {"name": "Alice"}, "age", 30] // → {"name": "Alice", "age": 30}
["set", {}, "key", "value"] // → {"key": "value"}
keys¶
Returns a list of all keys in the dictionary.
values¶
Returns a list of all values in the dictionary.
merge¶
["merge", {"a": 1}, {"b": 2}, {"c": 3}] // → {"a": 1, "b": 2, "c": 3}
["merge", {"a": 1}, {"a": 2}] // → {"a": 2}
has-key?¶
Returns true if the dictionary contains the specified key.
Arithmetic Operations¶
Mathematical operations with n-arity support for natural expression.
+ (Addition)¶
Adds all arguments. With no arguments, returns 0.
- (Subtraction)¶
Subtracts subsequent arguments from the first. With one argument, returns negation.
* (Multiplication)¶
Multiplies all arguments. With no arguments, returns 1.
/ (Division)¶
Divides the first argument by all subsequent arguments. With one argument, returns reciprocal.
mod (Modulo)¶
Returns the remainder of division.
pow (Exponentiation)¶
Raises the first argument to the power of the second.
Comparison Operations¶
Chained comparisons supporting mathematical notation.
= (Equality)¶
Returns true if all arguments are equal.
< (Less Than)¶
Returns true if arguments form an ascending sequence.
> (Greater Than)¶
Returns true if arguments form a descending sequence.
<= (Less Than or Equal)¶
Returns true if arguments form a non-decreasing sequence.
>= (Greater Than or Equal)¶
Returns true if arguments form a non-increasing sequence.
Logical Operations¶
Logical operations with n-arity support and short-circuiting.
and¶
Returns true if all arguments are truthy.
or¶
Returns true if any argument is truthy.
not¶
Returns the logical negation of the argument.
Type Predicates¶
Essential for wire format validation and dynamic type checking.
null?¶
Returns true if the value is null.
bool?¶
Returns true if the value is a boolean.
number?¶
Returns true if the value is a number (integer or float).
string?¶
Returns true if the value is a string.
list?¶
Returns true if the value is a list.
dict?¶
Returns true if the value is a dictionary.
callable?¶
Returns true if the value is callable (function or closure).
String Operations¶
String manipulation functions for text processing.
str-concat¶
["str-concat", "Hello", " ", "World"] // → "Hello World"
["str-concat", "Number: ", 42] // → "Number: 42"
str-split¶
["str-split", "a,b,c", ","] // → ["a", "b", "c"]
["str-split", "hello world"] // → ["hello", "world"] (default: space)
str-join¶
["str-join", ["a", "b", "c"], ","] // → "a,b,c"
["str-join", [1, 2, 3], "-"] // → "1-2-3"
["str-join", ["a", "b"]] // → "ab" (default: empty string)
str-length¶
Returns the length of a string.
str-upper¶
Converts a string to uppercase.
str-lower¶
Converts a string to lowercase.
Higher-Order Functions¶
The cornerstone of functional programming, enabling composition and abstraction.
map¶
["map", ["lambda", ["x"], ["*", "x", 2]], [1, 2, 3]] // → [2, 4, 6]
["map", "+", [[1, 2], [3, 4]]] // → [3, 7]
filter¶
["filter", ["lambda", ["x"], [">", "x", 5]], [1, 6, 3, 8, 2]] // → [6, 8]
["filter", "even?", [1, 2, 3, 4, 5, 6]] // → [2, 4, 6]
reduce¶
["reduce", "+", [1, 2, 3, 4]] // → 10
["reduce", "*", [1, 2, 3, 4], 1] // → 24 (with initial value)
["reduce", "max", [3, 1, 4, 1, 5]] // → 5
apply¶
Applies a function to a list of arguments.
Mathematical Functions¶
Extended mathematical operations for scientific computing.
min / max¶
Returns the minimum or maximum of the arguments.
abs¶
Returns the absolute value.
round¶
Rounds to the nearest integer or specified decimal places.
Trigonometric Functions¶
Standard trigonometric functions (arguments in radians).sqrt¶
Returns the square root.
log / exp¶
Natural logarithm and exponential functions.
Type Conversion¶
Safe type conversion functions with reasonable defaults.
to-string¶
Converts any value to its string representation.
to-number¶
["to-number", "42"] // → 42.0
["to-number", "3.14"] // → 3.14
["to-number", "hello"] // → 0 (safe default)
type-of¶
Returns the type name of a value.
I/O Operations¶
Basic I/O functions (can be customized in sandboxed environments).
print¶
["print", "Hello, World!"] // Outputs: Hello, World!
["print", 42, "is the answer"] // Outputs: 42 is the answer
error¶
Raises a runtime error with the specified message.
Integration with JSL Closures¶
All higher-order functions in the prelude work seamlessly with JSL closures through the eval_closure_or_builtin integration layer. This ensures that:
- Lexical scoping is preserved - Closures maintain access to their captured environments
- Built-in access is guaranteed - All closures can access prelude functions
- Performance is optimized - Environment chains are linked efficiently at call time
- Serialization is safe - Only user bindings are serialized with closures
This design enables powerful functional programming patterns while maintaining JSL's core promise of safe, network-transmissible code.