Menu

Standard Library Modules

Relevant source files

This document covers the core standard library modules that provide fundamental functionality for OwnLang programs. These modules implement essential capabilities such as string manipulation, mathematical operations, file I/O, functional programming constructs, and type utilities. For information about the module loading mechanism and interface design, see Module Architecture. For specialized modules like HTTP clients and graphics, see Network and HTTP Modules and Graphics and UI Modules.

Module Overview

The standard library consists of several core modules that are commonly imported together to provide a comprehensive foundation for OwnLang applications. Each module implements the standard Module interface and provides both constants and functions through the constants() and functions() methods.

Sources: examples/network/twitch_tools.own2 examples/versions/whatsnew_1.5.0.own1

The std Module

The std module provides core string manipulation and utility functions essential for most OwnLang programs. It serves as the foundational module for basic operations.

Key Functions

FunctionPurposeExample Usage
getBytes()Convert string to byte arraygetBytes("Hello")
stringFromBytes()Convert byte array to stringstringFromBytes([119, 111, 114, 108, 100])
stripMargin()Remove margin from multi-line strings`text.stripMargin("
contains()Check if string contains substringcontains(haystack, needle)
indexOf()Find substring positionindexOf(string, substring)
substring()Extract substringsubstring(text, start, end)
replace()Replace text occurrencesreplace(text, old, new)
split()Split string into arraysplit(text, delimiter)
toLowerCase()Convert to lowercasetoLowerCase(text)
urlencode()URL encode stringurlencode(url)

String Processing Features

The std module implements advanced string processing capabilities including encoding support and margin handling for formatted text blocks.

Sources: examples/versions/whatsnew_1.5.0.own4-8 examples/versions/whatsnew_1.5.0.own10-18 examples/network/twitch_tools.own104 examples/network/twitch_tools.own160-164 examples/network/twitch_tools.own181

The math Module

The math module provides mathematical functions and constants required for numerical computations.

Mathematical Operations

The module supports common mathematical operations including rounding, ceiling functions, random number generation, and provides standard mathematical constants.

Sources: examples/network/twitch_tools.own67 examples/network/twitch_tools.own92 examples/network/twitch_tools.own127

The functional Module

The functional module implements stream processing and higher-order functions for functional programming patterns in OwnLang.

Stream Processing Pipeline

Stream Operations

OperationPurposeExample
stream()Create stream from datastream(range(1, 10))
filter()Filter elements.filter(def(x) = x % 2 == 0)
joining()Join elements to string.joining(", ")
reduce()Reduce to single value.reduce(accumulator, initial)
forEach()Execute for each element.forEach(action)

The joining() function supports customizable separators and optional prefix/suffix formatting.

Sources: examples/versions/whatsnew_1.5.0.own22-27 examples/network/twitch_tools.own2

The types Module

The types module provides type checking and conversion utilities for runtime type validation and coercion.

Type System Support

The module enables dynamic type checking and provides utilities for working with OwnLang's flexible type system, supporting operations on arrays, objects, and primitive types.

Sources: examples/network/twitch_tools.own2

The json Module

The json module handles JSON serialization and deserialization with support for formatted output.

JSON Processing Functions

FunctionPurposeUsage
jsonencode()Serialize to JSONjsonencode(data)
jsonencode()Pretty-print JSONjsonencode(data, 2)
jsondecode()Parse JSONjsondecode(jsonString)

The module supports both compact and pretty-printed JSON output with configurable indentation levels.

Sources: examples/versions/whatsnew_1.5.0.own78-81 examples/network/twitch_tools.own72 examples/network/twitch_tools.own107 examples/network/twitch_tools.own148

The date Module

The date module provides comprehensive date and time manipulation capabilities with timezone support.

Date Operations

Key Date Functions

FunctionPurposeExample
parseDate()Parse date stringparseDate(str, format)
formatDate()Format date objectformatDate(date, format)
newFormat()Create date formatnewFormat("yyyy-MM-dd")
newDate()Create date objectnewDate(timestamp)
toTimestamp()Convert to timestamptoTimestamp(date)

The module supports ISO 8601 date formats and custom formatting patterns with timezone handling.

Sources: examples/network/twitch_tools.own51 examples/network/twitch_tools.own80 examples/network/twitch_tools.own139 examples/network/twitch_tools.own151 examples/network/twitch_tools.own182-183

The gzip Module

The gzip module provides data compression and decompression using the GZIP algorithm.

Compression Functions

FunctionPurposeUsage
gzipBytes()Compress byte arraygzipBytes(byteArray)
ungzipBytes()Decompress byte arrayungzipBytes(compressedBytes)

The module works with byte arrays and integrates with the std module's getBytes() and stringFromBytes() functions for text compression workflows.

Sources: examples/versions/whatsnew_1.5.0.own84-91

The java Module

The java module enables Java interoperability by providing access to Java classes and objects from OwnLang code.

Java Integration

FunctionPurposeExample
newClass()Create Java class referencenewClass("java.util.Random")

The module enables instantiation of Java objects and calling their methods directly from OwnLang code, bridging the gap between OwnLang and the Java ecosystem.

Sources: examples/versions/whatsnew_1.5.0.own95-97

Module Usage Patterns

Standard library modules are typically imported together using the use statement to provide a comprehensive foundation for application development.

The modules work together seamlessly, with std providing the foundation, functional enabling stream processing, and specialized modules like json and date handling specific data formats and operations.

Sources: examples/network/twitch_tools.own2 examples/versions/whatsnew_1.5.0.own1