Pome includes a standard library with built-in functions and modules.
Output values to the console.
print("Hello");
print("Value:", 42);
Returns the number of elements in a list, table, or string.
len([1, 2, 3]); // 3
len("hello"); // 5
Returns the type of a value as a string: "number", "string", "boolean", "nil", "list", "table", "function", "instance", "class", "module".
Import with import math;.
| Function | Description |
|---|---|
math.sin(x) |
Sine of x (radians) |
math.cos(x) |
Cosine of x (radians) |
math.sqrt(x) |
Square root of x |
math.abs(x) |
Absolute value of x |
math.floor(x) |
Largest integer <= x |
math.ceil(x) |
Smallest integer >= x |
math.random() |
Random number between 0 and 1 |
Constants: math.pi
Import with import io;.
| Function | Description |
|---|---|
io.readFile(path) |
Reads entire file as a string. Returns nil on failure. |
io.writeFile(path, content) |
Writes string to file. Returns true on success. |
io.input(prompt) |
Prints prompt and reads a line from stdin. |
Import with import string;.
| Function | Description |
|---|---|
string.sub(str, start, [len]) |
Returns a substring. |
string.lower(str) |
Returns lowercase version of string. |
string.upper(str) |
Returns uppercase version of string. |
Import with import time;.
| Function | Description |
|---|---|
time.clock() |
Returns monotonic time in seconds since an arbitrary point. |
time.sleep(seconds) |
Pauses execution for the specified number of seconds. |
Import with import list;.
| Function | Description |
|---|---|
list.push(list, value) |
Appends value to the end of the list. |
list.pop(list) |
Removes and returns the last element of the list. |
Returns the total number of objects currently managed by the garbage collector.
print("Objects in memory:", gc_count());
Manually triggers a full garbage collection cycle.
gc_collect();
Mathematical functions and constants.
import math;
print(math.pi); // Output: 3.14159...
import math;
print(math.sin(0)); // Output: 0
print(math.sin(math.pi / 2)); // Output: 1
print(math.cos(0)); // Output: 1
print(math.cos(math.pi)); // Output: -1
import math;
print(math.random()); // Random value between 0 and 1
var randInt = math.random() * 100; // Random value between 0 and 100
String manipulation functions.
Extract a substring.
import string;
var text = "Hello World";
print(string.sub(text, 0, 5)); // Output: Hello
print(string.sub(text, 6, 11)); // Output: World
Parameters:
str: The source stringstart: Starting index (0-based)end: Ending index (exclusive)
File input/output operations.
Read the contents of a file.
import io;
var content = io.readFile("myfile.txt");
print(content);
Returns: String containing the file contents
Write content to a file.
import io;
var success = io.writeFile("output.txt", "Hello from Pome");
if (success) {
print("File written successfully");
}
Parameters:
path: File path to write tocontent: Content to write
Returns: Boolean indicating success
// Empty list
var empty = [];
// List with values
var numbers = [1, 2, 3];
// Empty table
var emptyTable = {};
// Table with values
var person = {name: "Alice", age: 30};
fun processValue(val) {
var t = type(val);
if (t == "number") {
print("Processing number:", val);
} else if (t == "string") {
print("Processing string:", val);
} else if (t == "list") {
print("Processing list with", len(val), "items");
} else if (t == "table") {
print("Processing table");
}
}
While Pome doesn't have a full string library built-in, you can implement common operations:
var text = "Hello";
print(len(text)); // Output: 5
var greeting = "Hello" + " " + "World";
print(greeting); // Output: Hello World
import string;
var text = "Hello World";
var part = string.sub(text, 0, 5); // Output: Hello
var empty = [];
var numbers = [1, 2, 3, 4, 5];
var mixed = [1, "two", 3.0, true];
var items = ["a", "b", "c"];
print(items[0]); // Output: a
print(items[-1]); // Output: c
var items = [1, 2, 3];
print(len(items)); // Output: 3
var list1 = [1, 2];
var list2 = [3, 4];
var combined = list1 + list2;
print(combined); // Output: [1, 2, 3, 4]
var numbers = [1, 2, 3, 4, 5];
print(numbers[1:3]); // Output: [2, 3]
print(numbers[2:]); // Output: [3, 4, 5]
var empty = {};
var person = {name: "Bob", age: 25};
var person = {name: "Charlie", age: 30};
print(person.name); // Output: Charlie
print(person["name"]); // Output: Charlie
var config = {};
config.timeout = 5000;
config["retries"] = 3;
var person = {name: "Diana"};
if (person.email == nil) {
print("Email not set");
}
fun map(list, fn) {
var result = [];
for (var i = 0; i < len(list); i = i + 1) {
result = result + [fn(list[i])];
}
return result;
}
var numbers = [1, 2, 3];
var doubled = map(numbers, fun(x) { return x * 2; });
print(doubled); // Output: [2, 4, 6]
fun filter(list, predicate) {
var result = [];
for (var i = 0; i < len(list); i = i + 1) {
if (predicate(list[i])) {
result = result + [list[i]];
}
}
return result;
}
var numbers = [1, 2, 3, 4, 5];
var evens = filter(numbers, fun(x) { return x % 2 == 0; });
print(evens); // Output: [2, 4]
fun reduce(list, initial, fn) {
var acc = initial;
for (var i = 0; i < len(list); i = i + 1) {
acc = fn(acc, list[i]);
}
return acc;
}
var numbers = [1, 2, 3, 4];
var sum = reduce(numbers, 0, fun(acc, x) { return acc + x; });
print(sum); // Output: 10
import io;
// Write a file
io.writeFile("data.txt", "Hello, World!");
// Read the file
var content = io.readFile("data.txt");
print(content); // Output: Hello, World!
import io;
import string;
var content = io.readFile("input.txt");
var length = len(content);
print("File is", length, "characters long");
// Extract first line
var firstLine = string.sub(content, 0, 10);
print("First 10 chars:", firstLine);
import io;
var content = io.readFile("possibly_missing.txt");
if (content == nil) {
print("File not found");
} else {
print("File content:", content);
}
fun safeSum(values) {
if (type(values) != "list") {
return nil;
}
var total = 0;
for (var i = 0; i < len(values); i = i + 1) {
var item = values[i];
if (type(item) == "number") {
total = total + item;
}
}
return total;
}
var myString = "not a list";
print(safeSum(myList)); // Output: 6
print(safeSum(mixedList)); // Output: 4 (skips "two")
print(safeSum(myString)); // Output: nil
Avoid repeated concatenation in loops:
// Less efficient (and not supported with '+' for lists anyway)
// var result = [];
// for (var i = 0; i < 1000; i = i + 1) {
// result = result + [i]; // Creates new list each time, also list concatenation is not supported
// }
// Pome supports dynamic list resizing when assigning to the index equal to the length (appending).
var result = [];
for (var i = 0; i < 5; i = i + 1) {
result[i] = i; // Appends to the list
}
print(result); // Output: [0, 1, 2, 3, 4]
// Multiple concatenations
var str = "a" + "b" + "c" + "d";
// More efficient single concatenation
var str = "a" + "b" + "c" + "d";
Next: Error Handling
Back to: Documentation Home