ArraySlice

public struct ArraySlice[T] { /* private fields */ }

Non-owning view over a contiguous run of T values.

Slice is the standard "borrow" type for arrays, buffers, and any other contiguous storage: it stores a pointer + length and provides safe and unchecked indexing, sub-slicing, iteration, and pattern matching. The slice does not track or extend the lifetime of the underlying storage — keeping a slice past the end of its source is a use-after-free.

Examples

let arr = [1, 2, 3, 4]; let s = arr.asSlice(); s[safe: 0] // .Some(1) s[safe: 99] // .None for x in s.iter() { print(x) }

Memory Model

Non-owning. Drop the source (Array, Buffer, literal scope) and the slice becomes dangling. Slices freely copy — they're just (ptr, len) pairs.

Properties

public var count: Int64 { get }

Element count.

public var isEmpty: Bool { get }

true when count == 0.

public var pointer: Pointer[T] { get }

Pointer to the first element. pointer.offset(by: i) reaches element i (0-indexed).

Initializers

public init(pointer: Pointer[T], count: Int64)

Builds a slice from an existing pointer and element count. The caller is responsible for ensuring count elements live at pointer.

Methods

public func first() -> Optional[T]

First element, or .None for an empty slice.

public func last() -> Optional[T]

Last element, or .None for an empty slice.

ImplementsArrayMatchable

Associated Types

type Element = T

Methods

public func matchGet(Int64) -> T

Compiler-driven element read; safe to skip the bounds check because the matcher emits index < matchLength() first.

public func matchLength() -> Int64

Element count, exposed to the pattern matcher.

public func matchSlice(Int64, Int64) -> ArraySlice[T]

Sub-slice for rest-pattern bindings (..rest). As above, the matcher guarantees 0 <= from <= to <= matchLength().

ImplementsSlice

Properties

public var count: Int64 { get }

Element count. O(1).

Examples

[1, 2, 3].count; // 3 [].count; // 0
public var indices: Range[Int64] { get }

Half-open range 0..<count.

Examples

[10, 20, 30].indices; // 0..<3
public var isEmpty: Bool { get }

true when count == 0.

Examples

[].isEmpty; // true [1].isEmpty; // false

Methods

public func all(where: (T) -> Bool) -> Bool

true when every element satisfies predicate. O(n).

Short-circuits on the first failure. Vacuously true for empty collections.

Examples

[2, 4, 6].all(where: { it % 2 == 0 }); // true [2, 3, 6].all(where: { it % 2 == 0 }); // false
public func any(where: (T) -> Bool) -> Bool

true when at least one element satisfies predicate. O(n).

Short-circuits on the first match. Always false for empty collections.

Examples

[1, 2, 3].any(where: { it > 2 }); // true [1, 2, 3].any(where: { it > 5 }); // false
public func asPointer() -> Pointer[T]

Pointer to the first element. The pointer aliases the collection's buffer; do not outlive the source or mutate through it.

Safety

Reading past count is undefined behavior.

public func asSlice() -> ArraySlice[T]

Returns selfArraySlice is already the borrowed view.

public func binarySearch(T) -> Int64?

Binary search for element. Returns its index or None. O(log n).

When duplicates exist, which index is returned is unspecified.

Safety

The collection must be sorted in ascending order. Calling on unsorted data won't crash but may produce false negatives.

Examples

[1, 2, 3, 4, 5].binarySearch(3); // Some(2) [1, 2, 3, 4, 5].binarySearch(6); // None
public func chunks(of: Int64) -> ChunksView[T]

Multi-pass lazy view over non-overlapping size-sized chunks.

The trailing chunk may be shorter than size. Multi-pass: query count, index with view.get(i), and iterate repeatedly without re-creating the view.

Errors

Panics if size <= 0.

Examples

let v = [1, 2, 3, 4, 5].chunks(of: 2); v.count; // 3 v.get(2); // ArraySlice[5] for c in v { ... }
public func compactMap[U]((T) -> Optional[U]) -> Array[U]

Maps every element through transform, dropping .None results. O(n).

Examples

["1", "x", "3"].compactMap { Int64.parse(it) }; // [1, 3]
public func contains(T) -> Bool

true if the collection contains element. O(n).

Linear scan; short-circuits on the first match.

Examples

[1, 2, 3].contains(2); // true [1, 2, 3].contains(5); // false
public func countItems(where: (T) -> Bool) -> Int64

Number of elements for which predicate is true. O(n).

Examples

[1, 2, 3, 4, 5].countItems(where: { it % 2 == 0 }); // 2
public func drop(first: Int64) -> ArraySlice[T]

Returns a slice with the first count elements skipped. O(1).

Complement of prefix.

Errors

Panics if count > self.count.

Examples

[1, 2, 3, 4, 5].drop(first: 2); // ArraySlice[3, 4, 5]
public func ends[__opaque_0](with: __opaque_0) -> Bool where __opaque_0: Slice[T]

true if the trailing elements match suffix. O(k) where k is the suffix length. Accepts any Slice[T] conformer.

Examples

[1, 2, 3].ends(with: [2, 3]); // true [1, 2, 3].ends(with: [1, 2]); // false [1, 2, 3].ends(with: []); // true (vacuous)
public mutating func ensureUnique()

No-op — ArraySlice is a non-owning view with no COW barrier.

public func filter(where: (T) -> Bool) -> Array[T]

Returns a new array containing every element matching predicate. O(n). Result size is unknown; uses geometric growth.

Examples

[1, 2, 3, 4].filter(where: { it % 2 == 0 }); // [2, 4]
public func first() -> T?

First element, or .None for an empty collection. O(1).

Read-only — to remove the first element from an Array, use popFirst().

Examples

[1, 2, 3].first(); // Some(1) [].first(); // None
public func firstIndex(where: (T) -> Bool) -> Int64?

Index of the first element matching predicate, or None. O(n).

Short-circuits on the first match. For value-based search on Equatable collections, use firstIndex(of:).

Examples

[1, 2, 3, 4, 5].firstIndex(where: { it > 3 }); // Some(3) [1, 2, 3].firstIndex(where: { it > 10 }); // None
public func flatMap[U]((T) -> Array[U]) -> Array[U]

Maps every element through transform and concatenates the results into one flat array. O(n + total_output).

Examples

[1, 2, 3].flatMap { [it, it * 10] }; // [1, 10, 2, 20, 3, 30]
public func format(into: mutating StringBuilder, FormatOptions)

Renders as "[e1, e2, ...]". Empty collections render as "[]".

Examples

[1, 2, 3].format(); // "[1, 2, 3]" [].format(); // "[]"
public func isEqual(to: Self) -> Bool

Element-wise equality. O(n).

Short-circuits on the first mismatch. Order matters.

Examples

[1, 2, 3].isEqual(to: [1, 2, 3]); // true [1, 2, 3].isEqual(to: [3, 2, 1]); // false
public func isSorted() -> Bool

true if elements are in non-decreasing order. O(n).

Equal adjacent elements are allowed. Empty and single-element collections are vacuously sorted.

Examples

[1, 2, 3].isSorted(); // true [1, 3, 2].isSorted(); // false [1, 1, 1].isSorted(); // true [].isSorted(); // true
public func isValidIndex(Int64) -> Bool

true if index is in [0, count).

Examples

[10, 20, 30].isValidIndex(2); // true [10, 20, 30].isValidIndex(3); // false [10, 20, 30].isValidIndex(-1); // false
public func iter() -> ArraySliceIterator[T]

Forward iterator over the elements.

Examples

for item in [1, 2, 3] { ... }
public func last() -> T?

Last element, or .None for an empty collection. O(1).

Read-only — to remove the last element from an Array, use pop().

Examples

[1, 2, 3].last(); // Some(3) [].last(); // None
public func lastIndex(where: (T) -> Bool) -> Int64?

Index of the last element matching predicate, or None. O(n).

Scans from the back; short-circuits on the first match.

Examples

[1, 2, 3, 2, 1].lastIndex(where: { it == 2 }); // Some(3)
public func map[U]((T) -> U) -> Array[U]

Maps every element through transform into a new array. O(n).

Pre-sizes the result buffer to self.count, so no growth steps. For the lazy version that fuses into a chain, use iter().map { ... }.

Examples

[1, 2, 3].map { it * 2 }; // [2, 4, 6] [1, 2, 3].map { it.format() }; // ["1", "2", "3"]
public func max() -> T?

Largest element, or None if empty. O(n).

Ties go to the first occurrence.

Examples

[3, 1, 4].max(); // Some(4) [].max(); // None
public func min() -> T?

Smallest element, or None if empty. O(n).

Ties go to the first occurrence.

Examples

[3, 1, 4].min(); // Some(1) [].min(); // None
public func prefix(Int64) -> ArraySlice[T]

Returns a slice over the first count elements. O(1).

Errors

Panics if count > self.count.

Examples

[1, 2, 3, 4, 5].prefix(3); // ArraySlice[1, 2, 3] [1, 2].prefix(0); // empty slice
public func reversed() -> ReversedView[T]

Multi-pass lazy reversed view. Iterates back-to-front and supports indexed access in O(1).

Examples

let v = [1, 2, 3].reversed(); v.first(); // Some(3) v.toArray(); // [3, 2, 1] — eager copy
public func sorted() -> Array[T]

Returns a new sorted array; original unchanged. O(n log n).

Examples

let arr = [3, 1, 4, 1, 5]; arr.sorted(); // [1, 1, 3, 4, 5] // arr is still [3, 1, 4, 1, 5]
public func split(where: consuming (T) -> Bool) -> ArraySplitWhereView[T]

Multi-pass lazy view over the segments produced by splitting at each element matching predicate. Matching elements are dropped.

Examples

let v = [1, -1, 2, 3, -1, 4].split(where: { it < 0 }); for seg in v { ... }
public func starts[__opaque_0](with: __opaque_0) -> Bool where __opaque_0: Slice[T]

true if the leading elements match prefix. O(k) where k is the prefix length. Accepts any Slice[T] conformer.

Examples

[1, 2, 3].starts(with: [1, 2]); // true [1, 2, 3].starts(with: [2, 3]); // false [1, 2, 3].starts(with: []); // true (vacuous)
public func suffix(Int64) -> ArraySlice[T]

Returns a slice over the last count elements. O(1).

Errors

Panics if count > self.count.

Examples

[1, 2, 3, 4, 5].suffix(2); // ArraySlice[4, 5]
public func unique() -> Array[T]

Returns a new array with duplicates removed, preserving first-occurrence order. O(n²).

For the mutating variant on Array, see removeDuplicates().

Examples

[1, 2, 1, 3, 2, 4].unique(); // [1, 2, 3, 4]
public func windows(of: Int64) -> WindowsView[T]

Multi-pass lazy view over overlapping size-sized sliding windows.

Adjacent windows overlap by size - 1 elements. Empty when the source has fewer than size elements.

Errors

Panics if size <= 0.

Examples

let v = [1, 2, 3, 4].windows(of: 2); v.count; // 3 for w in v { ... }

Subscripts

public subscript[I](I) -> I.SeqOutput { get set }

ImplementsIterable

Associated Types

type Item = T
type TargetIterator = ArraySliceIterator[T]

Methods

public func iter() -> ArraySliceIterator[T]

Forward iterator over the elements.

ImplementsEquatable

Associated Types

type Output = Bool

Methods

public func equal(to: Self) -> Bool

Bridges Equal.equal(to:) to Equatable.isEqual(to:).

func isEqual(to: Self) -> Bool

Returns true iff self and other are considered equal. Should be reflexive, symmetric, and transitive — Hashable requires equal values to hash equal, so don't drift from those laws.

public func notEqual(to: Self) -> Bool

Default !=: delegates to == so there's a single source of truth.

Defined in lang/std/memory/pointer.ks