---
audience: engineers
summary: Built-in functions, types, and methods available to every flow.
title: Standard library
path: reference/language/standard-library
status: published
---

### 11.1 String Methods

All string methods are immutable — they return a new string, never modify
the original.

```
string.length: int                              // Property, not method
string.lower(): string                          // "Hello" → "hello"
string.upper(): string                          // "Hello" → "HELLO"
string.trim(): string                           // "  hi  " → "hi"
string.trim_start(): string                     // "  hi  " → "hi  "
string.trim_end(): string                       // "  hi  " → "  hi"
string.contains(sub: string): bool              // "hello".contains("ell") → true
string.starts_with(prefix: string): bool        // "hello".starts_with("he") → true
string.ends_with(suffix: string): bool          // "hello".ends_with("lo") → true
string.replace(old: string, new: string): string // "aaba".replace("a", "x") → "xxbx"
string.replace_first(old: string, new: string): string
string.split(sep: string): array[string]        // "a,b,c".split(",") → ["a","b","c"]
string.slice(start: int, end: int?): string     // "hello".slice(1,3) → "el"
string.index_of(sub: string): int?              // "hello".index_of("ll") → 2, null if not found
string.pad_start(length: int, fill: string = " "): string
string.pad_end(length: int, fill: string = " "): string
string.repeat(n: int): string                   // "ab".repeat(3) → "ababab"
string.matches(pattern: string): bool           // Simple glob matching (* and ?)
string.is_empty(): bool                         // "".is_empty() → true
```

### 11.2 Array Methods

Arrays are immutable in expressions. Methods that would "modify" return
a new array. The `fn` parameter is a lambda expression.

```
array[T].length: int                            // Property
array[T].first: T?                              // Null if empty
array[T].last: T?                               // Null if empty
array[T].is_empty(): bool

// Functional transforms (return new arrays)
array[T].map(fn: (T) => U): array[U]
array[T].filter(fn: (T) => bool): array[T]
array[T].flat_map(fn: (T) => array[U]): array[U]
array[T].sort(fn: ((T, T) => int)?): array[T]   // Comparator optional for primitives
array[T].reverse(): array[T]
array[T].unique(): array[T]                     // Remove duplicates (by value equality)
array[T].take(n: int): array[T]                 // First n elements
array[T].drop(n: int): array[T]                 // Skip first n elements
array[T].slice(start: int, end: int?): array[T]
array[T].zip(other: array[U]): array[{ first: T, second: U }]
array[T].group_by(fn: (T) => K): map[K, array[T]]
array[T].chunk(size: int): array[array[T]]

// Aggregation (reduce to single value)
array[T].sum(fn: ((T) => float)?): float        // fn optional for numeric arrays
array[T].min(fn: ((T) => float)?): T?
array[T].max(fn: ((T) => float)?): T?
array[T].count(fn: ((T) => bool)?): int         // Count matching, or total if no fn
array[T].any(fn: (T) => bool): bool             // True if any element matches
array[T].all(fn: (T) => bool): bool             // True if all elements match
array[T].none(fn: (T) => bool): bool            // True if no element matches
array[T].find(fn: (T) => bool): T?              // First match, or null
array[T].find_index(fn: (T) => bool): int?      // Index of first match, or null
array[T].reduce(fn: (acc: U, item: T) => U, initial: U): U

// Building
array[T].append(item: T): array[T]              // Returns new array with item added
array[T].prepend(item: T): array[T]
array[T].concat(other: array[T]): array[T]
array[T].insert(index: int, item: T): array[T]
array[T].remove_at(index: int): array[T]

// Conversion
array[T].join(sep: string): string              // ["a","b"].join(",") → "a,b"
array[T].to_map(key_fn: (T) => K, val_fn: (T) => V): map[K, V]

// Access
array[T][index: int]: T                         // Index access (0-based, negative from end)
array[T].contains(item: T): bool                // Value equality check
```

### 11.3 Map Methods

```
map[K, V].size: int                             // Property
map[K, V].is_empty(): bool
map[K, V].keys(): array[K]
map[K, V].values(): array[V]
map[K, V].entries(): array[{ key: K, value: V }]
map[K, V].contains_key(key: K): bool
map[K, V].get(key: K): V?                       // Null if not found
map[K, V].get_or(key: K, default: V): V
map[K, V][key: K]: V                            // Index access (throws if not found)

// Transforms (return new maps)
map[K, V].set(key: K, value: V): map[K, V]     // Returns new map with key set
map[K, V].remove(key: K): map[K, V]            // Returns new map with key removed
map[K, V].merge(other: map[K, V]): map[K, V]   // other's values win on conflict
map[K, V].map_values(fn: (V) => U): map[K, U]
map[K, V].filter(fn: (K, V) => bool): map[K, V]
```

### 11.4 Math Functions

```
abs(x: float | int): float | int
min(a: float | int, b: float | int): float | int
max(a: float | int, b: float | int): float | int
round(x: float, decimals: int = 0): float
floor(x: float): int
ceil(x: float): int
clamp(x: float, low: float, high: float): float
```

### 11.5 Type Conversion Functions

```
string(value: any): string              // Any value to its string representation
int(value: string | float | bool): int  // Parse/convert to int
float(value: string | int): float       // Parse/convert to float
bool(value: string | int): bool         // "true"/1 → true, "false"/0 → false
```

### 11.6 Date and Time Functions

```
now(): datetime                         // Current UTC datetime
today(): date                           // Current UTC date
date(year: int, month: int, day: int): date
datetime(year: int, month: int, day: int, hour: int = 0, min: int = 0, sec: int = 0): datetime
duration(days: int = 0, hours: int = 0, minutes: int = 0, seconds: int = 0, ms: int = 0): duration
parse_date(s: string, format: string?): date        // ISO 8601 default
parse_datetime(s: string, format: string?): datetime

// Date/datetime methods
date.year: int
date.month: int
date.day: int
date.day_of_week: int                   // 1=Monday, 7=Sunday
date.format(pattern: string): string
date.add(d: duration): date
date.diff(other: date): duration

datetime.date: date                     // Extract date part
datetime.hour: int
datetime.minute: int
datetime.second: int
datetime.timestamp_ms: int              // Unix milliseconds
datetime.format(pattern: string): string
datetime.add(d: duration): datetime
datetime.diff(other: datetime): duration
datetime.is_before(other: datetime): bool
datetime.is_after(other: datetime): bool

duration.total_ms: int
duration.total_seconds: float
duration.total_minutes: float
duration.total_hours: float
duration.total_days: float
```

### 11.7 Money Methods

```
money.amount: float
money.currency: string                  // ISO 4217 code
money.abs(): money
money.round(decimals: int = 2): money
money.format(): string                  // "€1,234.56" (locale-aware)
```

### 11.8 Utility Functions

```
generate_id(): uuid                     // Random UUID v4
diff(a: string, b: string): string      // Text diff (for logging/debugging)
hash(value: string, algo: string = "sha256"): string
json_encode(value: any): string         // Serialize to JSON string
json_decode(s: string): any             // Parse JSON string (returns object/array/primitive)
base64_encode(data: bytes): string
base64_decode(s: string): bytes
url_encode(s: string): string
url_decode(s: string): string
```

### 11.9 Logging

```scaicore
log.info(message: string): void
log.warn(message: string): void
log.error(message: string, error: Error?): void
log.debug(message: string): void        // Only in @debug blocks or debug builds
```

### 11.10 Tracing

```
trace.start(operation: string): void    // Start a named span
trace.end(): void                       // End the current span
trace.tag(key: string, value: string): void
```

### 11.11 Execution Context

Read-only properties available in all flows:

```
execution.id: string                    // Unique execution ID
execution.origin: ExecutionOrigin       // How this execution was triggered
execution.started_at: datetime          // When this execution started
execution.usage: UsageMetrics           // Current metering counters
```

### 11.12 Instance Discovery

Available in entity mode only (see Instance Lifecycle spec):

```
core.instances.list_active(): array[InstanceInfo]
core.instances.exists(key: string): bool
core.instances.count_active(): int
```

### 11.13 Events

`emit` is a statement, not a function (see §8.1):

```scaicore
emit event_name { field1 = value1, field2 = value2 }
```
# ScaiCore Language Dictionary v0.1

---