Logos / Docs

Standard Library

Logos comes with a set of standard library modules that provide common functionality. Import them using the use keyword.

use "std/array"
use "std/math"
use "std/string"
use "std/path"
use "std/time"
use "std/log"
use "std/type"
use "std/testing"

Standard library functions are written in Logos itself and build on top of the builtin functions.

std/array

Functional array utilities for transforming, filtering, and aggregating arrays.

Functions

FunctionDescription
map(arr, fn)Applies function to each element, returns new array
filter(arr, fn)Returns elements where function returns true
reduce(arr, fn, initial)Reduces array to single value
unique(arr)Returns array with duplicates removed
flat(arr)Flattens nested arrays
copy(arr)Creates a shallow copy of array
sum(arr)Returns sum of numeric elements
min(arr)Returns minimum value
max(arr)Returns maximum value
chunk(arr, size)Splits array into chunks of given size
indexOf(arr, val)Returns index of value, -1 if not found
containsAll(arr, items)Checks if array contains all items

Also available with array prefix: arrayMap, arrayFilter, arrayReduce, arraySum, etc.

Example

use "std/array"

let nums = [1, 2, 3, 4, 5]

// Map: double each number
let doubled = map(nums, fn(x) -> x * 2)
print(toStr(doubled))  // [2, 4, 6, 8, 10]

// Filter: keep even numbers
let evens = filter(nums, fn(x) -> x % 2 == 0)
print(toStr(evens))  // [2, 4]

// Reduce: sum all numbers
let total = reduce(nums, fn(acc, x) -> acc + x, 0)
print(toStr(total))  // 15

// Other utilities
print(toStr(sum(nums)))           // 15
print(toStr(min(nums)))           // 1
print(toStr(max(nums)))           // 5
print(toStr(unique([1, 2, 2, 3]))) // [1, 2, 3]
print(toStr(chunk(nums, 2)))      // [[1, 2], [3, 4], [5]]

std/math

Mathematical functions and algorithms.

Functions

FunctionDescription
mathFib(n)Returns nth Fibonacci number
mathFactorial(n)Returns factorial of n
mathMean(arr)Returns average of array
mathMedian(arr)Returns median of array
mathMode(arr)Returns most frequent value
mathClamp(n, min, max)Clamps value to range
mathLerp(a, b, t)Linear interpolation
mathIsPrime(n)Returns true if n is prime
mathGcd(a, b)Greatest common divisor
mathLcm(a, b)Least common multiple

Example

use "std/math"

print(toStr(mathFactorial(5)))  // 120
print(toStr(mathFib(10)))       // 55
print(toStr(mathIsPrime(17)))   // true

let scores = [85, 90, 78, 92, 88]
print(toStr(mathMean(scores)))    // 86.6
print(toStr(mathMedian(scores)))  // 88

print(toStr(mathClamp(150, 0, 100)))  // 100
print(toStr(mathGcd(48, 18)))         // 6
print(toStr(mathLcm(4, 6)))           // 12

std/string

String manipulation utilities.

Functions

FunctionDescription
strPadStart(s, len, char)Pads string at start to length
strPadEnd(s, len, char)Pads string at end to length
strCapitalize(s)Capitalizes first character
strIsEmpty(s)Returns true if string is empty/whitespace
strCount(s, sub)Counts occurrences of substring
strReverse(s)Reverses the string
strIsPalindrome(s)Returns true if string is palindrome
strRepeat(s, n)Repeats string n times

Example

use "std/string"

print(strReverse("hello"))         // olleh
print(strCapitalize("hello"))      // Hello
print(toStr(strIsPalindrome("racecar")))  // true

print(strPadStart("42", 5, "0"))   // 00042
print(strPadEnd("hi", 5, "!"))     // hi!!!

print(toStr(strCount("banana", "a")))  // 3
print(strRepeat("ab", 3))              // ababab

std/path

File path manipulation utilities.

Functions

FunctionDescription
pathJoin(parts)Joins path components (array)
pathBase(path)Returns filename portion
pathDir(path)Returns directory portion
pathExt(path)Returns file extension
pathIsAbs(path)Returns true if path is absolute
pathClean(path)Cleans path (resolves .. and .)
pathExists(path)Returns true if path exists
pathWithoutExt(path)Returns path without extension

Example

use "std/path"

let p = "/home/user/docs/file.txt"

print(pathBase(p))        // file.txt
print(pathDir(p))         // /home/user/docs
print(pathExt(p))         // .txt
print(pathWithoutExt(p))  // /home/user/docs/file

print(toStr(pathIsAbs(p)))      // true
print(toStr(pathExists(".")))   // true

// Join path components
let joined = pathJoin(["/home", "user", "docs"])
print(joined)  // /home/user/docs

// Clean messy paths
print(pathClean("/home/user/../user/docs"))  // /home/user/docs

std/time

Date and time utilities with datetime objects.

Functions

FunctionDescription
datetimeNow()Returns current datetime object
datetimeFromTimestamp(ts)Creates datetime from Unix timestamp
datetimeFormat(dt, fmt)Formats datetime with Go layout
datetimeIsAfter(dt1, dt2)Returns true if dt1 is after dt2
datetimeIsBefore(dt1, dt2)Returns true if dt1 is before dt2
datetimeIsEqual(dt1, dt2)Returns true if datetimes are equal
datetimeDiff(dt1, dt2, unit)Returns difference in specified unit
datetimeAdd(dt, amount, unit)Adds time to datetime
datetimeSubtract(dt, amount, unit)Subtracts time from datetime
datetimeToStr(dt)Returns datetime as string

Units: "seconds", "minutes", "hours", "days", "weeks"

Example

use "std/time"

let now = datetimeNow()
print(now["str"])    // 2026-03-02 14:30:00
print(now["date"])   // 2026-03-02
print(now["time"])   // 14:30:00

// Add time
let tomorrow = datetimeAdd(now, 1, "days")
let nextWeek = datetimeAdd(now, 7, "days")

// Format with custom layout
print(datetimeFormat(now, "Jan 2, 2006"))  // Mar 2, 2026

// Compare datetimes
print(toStr(datetimeIsAfter(tomorrow, now)))  // true

// Calculate difference
let diff = datetimeDiff(nextWeek, now, "days")
print(toStr(diff))  // 7

std/log

Structured logging with levels and optional file output.

Functions

FunctionDescription
logDebug(msg)Logs debug message (white)
logInfo(msg)Logs info message (green)
logWarn(msg)Logs warning message (yellow)
logError(msg)Logs error message (red)

Example

use "std/log"

logDebug("Starting application...")
logInfo("Server listening on port 8080")
logWarn("Configuration file not found, using defaults")
logError("Failed to connect to database")

// Output includes timestamp and level:
// [2026-03-02 14:30:00] [INFO] Server listening on port 8080

std/type

Type checking utilities for runtime type inspection.

Functions

FunctionDescription
isNull(val)Returns true if value is null
isString(val)Returns true if value is string
isInt(val)Returns true if value is integer
isFloat(val)Returns true if value is float
isBool(val)Returns true if value is boolean
isArray(val)Returns true if value is array
isTable(val)Returns true if value is table
isNumber(val)Returns true if int or float
isCallable(val)Returns true if value is function

Example

use "std/type"

let validate = fn(value) {
    if isNull(value) {
        return "Value is required"
    }
    if !isNumber(value) {
        return "Value must be a number"
    }
    return null
}

print(toStr(isString("hello")))  // true
print(toStr(isInt(42)))          // true
print(toStr(isNumber(3.14)))     // true
print(toStr(isArray([1, 2, 3]))) // true
print(toStr(isCallable(fn() {}))) // true

std/testing

Simple testing framework for writing and running tests.

Functions

FunctionDescription
assert(name, got, expected)Asserts got equals expected
suite(name, fn)Defines a test suite
summary()Prints test results and exits

Example

test.lgs
use "std/testing"

let add = fn(a, b) -> a + b
let multiply = fn(a, b) -> a * b

suite("math", fn() {
    assert("add 1 + 2", add(1, 2), 3)
    assert("add negative", add(-1, 1), 0)
    assert("multiply", multiply(3, 4), 12)
})

suite("strings", fn() {
    assert("upper", upper("hello"), "HELLO")
    assert("len", len("test"), 4)
})

summary()

// Output on success:
// ok    math, strings    5 tests passed

// Output on failure:
// --- FAIL: add 1 + 2 (got=4 expected=3)
// FAIL
// failed: 1/5