ESC
Type to search...
S
Soli Docs

Core Functions

I/O operations, type conversion, array/hash manipulation, string functions, and math utilities.

I/O Functions

print(value)

Prints a value to standard output without a newline.

print("Hello")
print(" World")  # Output: Hello World
println(value)

Prints a value to standard output with a newline.

println("Hello World")
println(42)
input(prompt?)

Reads a line of input from the user. Returns the user's input as a String.

let name = input("Enter your name: ")
println("Hello, " + name)

Type Functions

type(value)

Returns the type name of a value as a string.

type(42)        # "int"
type("hello")   # "string"
type([1, 2, 3]) # "array"
type(null)      # "null"
str(value)

Convert a value to a string.

str(42)       # "42"
str(3.14)     # "3.14"
str(true)     # "true"
int(value)

Convert a value to an integer.

int("42")     # 42
int(3.7)      # 3
float(value)

Convert a value to a float.

float("3.14") # 3.14
float(42)     # 42.0
len(value)

Returns the length of a string, array, or hash. Works with both primitives and class instances.

len("hello")      # 5
len([1, 2, 3])    # 3
len({"a": 1})     # 1
let arr = Array.new(); arr.push(1); len(arr)  # 1

Array Functions

Tip: Most array functions are also available as Array class methods. Use arr.push(42) instead of push(arr, 42) for a more object-oriented style.

push(array, value) / array.push(value)

Add an element to the end of an array.

let arr = [1, 2]
push(arr, 3)     # arr is now [1, 2, 3]

# Or use class method style:
let arr2 = [1, 2]
arr2.push(3)     # arr2 is now [1, 2, 3]
pop(array) / array.pop()

Remove and return the last element from an array.

let arr = [1, 2, 3]
let last = pop(arr)  # last is 3, arr is [1, 2]

# Or use class method style:
let arr2 = [1, 2, 3]
let last2 = arr2.pop()  # last2 is 3, arr2 is [1, 2]
range(start, end, step?)

Creates an array of numbers from start to end (exclusive).

range(0, 5)      # [0, 1, 2, 3, 4]
range(1, 10, 2)  # [1, 3, 5, 7, 9]
range(5, 0, -1)  # [5, 4, 3, 2, 1]
clear(array | hash) / array.clear() / hash.clear()

Remove all elements from an array or all entries from a hash.

let arr = [1, 2, 3]
clear(arr)  # arr is now []

let h = {"a": 1, "b": 2}
h.clear()   # h is now {}

Hash Functions

Tip: Most hash functions are also available as Hash class methods. Use h.keys() instead of keys(h) for a more object-oriented style.

keys(hash) / hash.keys()

Returns an array of all keys in a hash.

let h = {"name": "Alice", "age": 30}
keys(h)       # ["name", "age"]
h.keys()      # ["name", "age"]
values(hash) / hash.values()

Returns an array of all values in a hash.

let h = {"name": "Alice", "age": 30}
values(h)     # ["Alice", 30]
h.values()    # ["Alice", 30]
entries(hash) / hash.entries()

Returns an array of [key, value] pairs.

let h = {"name": "Alice", "age": 30}
entries(h)    # [["name", "Alice"], ["age", 30]]
h.entries()   # [["name", "Alice"], ["age", 30]]
has_key(hash, key) / hash.has_key(key)

Check if a key exists in a hash.

has_key({"a": 1}, "a")    # true
has_key({"a": 1}, "b")    # false
let h = {"a": 1}; h.has_key("a")  # true
merge(h1, h2) / hash.merge(other)

Merge two hashes together. Values from h2 take precedence.

merge({"x": 1}, {"y": 2})     # {"x": 1, "y": 2}
let h1 = {"a": 1}; let h2 = {"a": 2}; h1.merge(h2)  # {"a": 2}
from_entries(array)

Create a hash from an array of [key, value] pairs.

from_entries([["a", 1], ["b", 2]])  # {"a": 1, "b": 2}

String Functions

Note: String functions are now primarily available as String class methods. Use "hello".split(",") instead of split("hello", ",").

str.split(sep)

Split a string by a separator. Returns an array of string parts.

"a,b,c".split(",")      # ["a", "b", "c"]
"hello world".split(" ")  # ["hello", "world"]
str.join(sep)

Join string parts with a separator.

"a,b,c".join("-")    # "a-b-c"
str.contains(sub)

Check if a string contains a substring.

"hello world".contains("world")  # true
"hello".contains("xyz")          # false
str.starts_with(prefix) / str.ends_with(suffix)

Check if a string starts with a prefix or ends with a suffix.

"hello".starts_with("hell")  # true
"hello".ends_with("llo")     # true
str.index_of(substr)

Returns the index of the first occurrence of substring, or -1 if not found.

"hello world".index_of("world")  # 6
"hello".index_of("xyz")          # -1
str.substring(start, end)

Extract a substring from start to end (exclusive).

"hello world".substring(0, 5)   # "hello"
"hello world".substring(6, 11)  # "world"
str.upcase() / str.downcase() / str.trim()

Transform string case and remove whitespace.

"hello".upcase()      # "HELLO"
"HELLO".downcase()    # "hello"
"  hi  ".trim()       # "hi"
str.replace(from, to)

Replace all occurrences of a substring.

"hello world".replace("world", "soli")  # "hello soli"
str.lpad(width, pad_char?) / str.rpad(width, pad_char?)

Pad a string to reach the specified width.

"hi".lpad(5)           # "   hi"
"hi".rpad(5)           # "hi   "
"hi".lpad(5, "0")      # "000hi"
String.new(value)

Create a String instance from various value types.

String.new(42)       # "42"
String.new(3.14)     # "3.14"
String.new(true)     # "true"

Math Functions

abs(n) / min(a, b) / max(a, b)

Common math operations.

abs(-5)       # 5
min(3, 7)     # 3
max(3, 7)     # 7
sqrt(n) / pow(base, exp)

Square root and power functions.

sqrt(16)      # 4.0
pow(2, 3)     # 8.0
clock()

Returns the current Unix timestamp as a float with sub-second precision.

let start = clock()
# ... do work ...
let elapsed = clock() - start

File I/O Functions

slurp(path)

Read an entire file into a string.

let content = slurp("config.json")
barf(path, content)

Write content to a file.

barf("output.txt", "Hello, World!")