Skip to content

Std\ByteArray

Contiguous unboxed byte array. Provides allocation, indexing, slicing, bulk operations (foldl, map), and conversion between byte arrays, strings, and sequences. Used for binary I/O, network protocols, and interop with C libraries. Implements the Array trait.

Allocate a zero-filled byte buffer of size bytes.

import alloc from Std\ByteArray in
let buf = alloc 1024 in
length buf # => 1024

Returns the number of bytes in the buffer.

import length from Std\ByteArray in
length (fromString "hello") # => 5

Returns the byte value (0-255) at the given index.

import get, fromString from Std\ByteArray in
let buf = fromString "ABC" in
get buf 0 # => 65

Sets the byte at index to value (0-255). Mutates the buffer in place.

import alloc, set, get from Std\ByteArray in
let buf = alloc 4 in
do
set buf 0 42
get buf 0 # => 42
end

concat : ByteArray -> ByteArray -> ByteArray

Section titled “concat : ByteArray -> ByteArray -> ByteArray”

Concatenate two byte buffers into a new buffer.

import concat, fromString from Std\ByteArray in
let buf = concat (fromString "hello ") (fromString "world") in
toString buf # => "hello world"

slice : ByteArray -> Int -> Int -> ByteArray

Section titled “slice : ByteArray -> Int -> Int -> ByteArray”

Extract a sub-buffer from index start (inclusive) to end (exclusive).

import slice, fromString, toString from Std\ByteArray in
toString (slice (fromString "hello") 1 4) # => "ell"

Convert a UTF-8 string to a byte buffer.

import fromString from Std\ByteArray in
let buf = fromString "hi" in
length buf # => 2

Convert a byte buffer back to a UTF-8 string.

import fromString, toString from Std\ByteArray in
toString (fromString "hello") # => "hello"

Create a byte buffer from a sequence of integers (0-255).

import fromSeq, get from Std\ByteArray in
let buf = fromSeq [72, 105] in
get buf 0 # => 72

Convert a byte buffer to a sequence of integers.

import fromString, toSeq from Std\ByteArray in
toSeq (fromString "Hi") # => [72, 105]

First byte value. O(1).

All bytes except the first. Returns a new array.

join : ByteArray -> ByteArray -> ByteArray

Section titled “join : ByteArray -> ByteArray -> ByteArray”

Concatenate two byte arrays (alias for concat).

foldl : (a -> b) -> Int -> ByteArray -> Int

Section titled “foldl : (a -> b) -> Int -> ByteArray -> Int”

Left fold over all bytes. Single-pass, cache-friendly.

import fromString, foldl from Std\ByteArray in
foldl (\acc b -> acc + b) 0 (fromString "ABC") -- 65+66+67 = 198

Apply a function to each byte, returning a new array.