Built-in Functions Reference¶
Complete reference for all built-in functions in CacaoLang.
Array Functions¶
len(array | string)¶
Returns the length of an array or string.
push(array, value)¶
Adds an element to the end of an array and returns the new array.
pop(array)¶
Removes and returns the last element from an array.
Higher-Order Functions¶
map(array, lambda)¶
Transforms each element of an array using a lambda function.
let numbers = [1, 2, 3, 4, 5];
let doubled = map(numbers, |x| => { return x * 2; });
print doubled; // [2, 4, 6, 8, 10]
filter(array, lambda)¶
Keeps only elements that satisfy the lambda condition.
let numbers = [1, 2, 3, 4, 5, 6];
let evens = filter(numbers, |x| => { return x % 2 == 0; });
print evens; // [2, 4, 6]
reduce(array, initial, lambda)¶
Combines array elements into a single value.
let numbers = [1, 2, 3, 4, 5];
let sum = reduce(numbers, 0, |acc, x| => { return acc + x; });
print sum; // 15
Math Functions¶
sqrt(number)¶
Returns the square root of a number.
pow(base, exponent)¶
Raises base to the power of exponent.
abs(number)¶
Returns the absolute value.
floor(number)¶
Rounds down to nearest integer.
ceil(number)¶
Rounds up to nearest integer.
round(number)¶
Rounds to nearest integer.
min(a, b)¶
Returns the smaller of two numbers.
max(a, b)¶
Returns the larger of two numbers.
Random Functions¶
random()¶
Returns a random float between 0 and 1.
random_int(min, max)¶
Returns a random integer between min and max (inclusive).
String Functions¶
str(value)¶
Converts a value to a string.
uppercase(string)¶
Converts string to uppercase.
lowercase(string)¶
Converts string to lowercase.
substr(string, start, length)¶
Extracts a substring.
split(string, delimiter)¶
Splits string into an array.
let sentence = "apple,banana,cherry";
let fruits = split(sentence, ",");
print fruits; // ["apple", "banana", "cherry"]
join(array, separator)¶
Joins array elements into a string.
Type Functions¶
typeof(value)¶
Returns the type of a value as a string.
print typeof(42); // number
print typeof("hello"); // string
print typeof(true); // bool
print typeof([1,2,3]); // array
int(value)¶
Converts to integer.
float(value)¶
Converts to floating-point number.
File I/O Functions¶
read_file(filename)¶
Reads entire file contents as a string.
write_file(filename, content)¶
Writes string to file (overwrites existing).
append_file(filename, content)¶
Appends string to end of file.
file_exists(filename)¶
Checks if file exists.
Input/Output Functions¶
input(prompt)¶
Reads a line of user input.
print(value)¶
Prints value to console with newline.
Bitwise Functions¶
bitwise_and(a, b)¶
Performs bitwise AND operation.
bitwise_or(a, b)¶
Performs bitwise OR operation.
bitwise_xor(a, b)¶
Performs bitwise XOR operation.
bitwise_not(a)¶
Performs bitwise NOT operation.
shift_left(value, bits)¶
Shifts bits left.
shift_right(value, bits)¶
Shifts bits right.
count_bits(number)¶
Counts the number of set bits (1s).
to_binary(number)¶
Converts number to binary string.
to_hex(number)¶
Converts number to hexadecimal string.
from_binary(string)¶
Converts binary string to number.
from_hex(string)¶
Converts hexadecimal string to number.