Skip to main content
Kira

Lists

The List[T] type is a fundamental data structure in Kira.

Creating Lists

// Empty list
let empty: List[i32] = Nil

// Single element
let single: List[i32] = Cons(42, Nil)

// Multiple elements (constructed right-to-left)
let nums: List[i32] = Cons(1, Cons(2, Cons(3, Nil)))

// Using standard library
let empty2: List[i32] = std.list.empty[i32]()
let single2: List[i32] = std.list.singleton[i32](42)

List Operations (std.list)

Length and Access

let len: i32 = std.list.length[i32](nums)  // 3

Transforming

// Map: transform each element
let doubled: List[i32] = std.list.map[i32, i32](
    nums,
    fn(x: i32) -> i32 { return x * 2 }
)

// Filter: keep elements matching a predicate
let evens: List[i32] = std.list.filter[i32](
    nums,
    fn(x: i32) -> bool { return x % 2 == 0 }
)

// Fold: reduce to a single value
let sum: i32 = std.list.fold[i32, i32](
    nums,
    0,
    fn(acc: i32, x: i32) -> i32 { return acc + x }
)

Reordering

let rev: List[i32] = std.list.reverse[i32](nums)  // [3, 2, 1]
let combined: List[i32] = std.list.concat[i32](nums, rev)

Slicing

let first_two: List[i32] = std.list.take[i32](nums, 2)  // [1, 2]
let rest: List[i32] = std.list.drop[i32](nums, 2)        // [3]

Searching

let found: Option[i32] = std.list.find[i32](
    nums,
    fn(x: i32) -> bool { return x > 1 }
)  // Some(2)

let has_even: bool = std.list.any[i32](
    nums,
    fn(x: i32) -> bool { return x % 2 == 0 }
)

let all_positive: bool = std.list.all[i32](
    nums,
    fn(x: i32) -> bool { return x > 0 }
)