Slide 8 of 37

Builtins

Length and bytes
proc main(): void {
	fruit := ["pear", "apple", "kiwi"]
	echo len(fruit)
	echo bytes("café")
}
Growing and shrinking
proc main(): void {
	mut Str[dyn] basket = ["pear"]
	append(basket, "apple")
	prepend(basket, "kiwi")
	echo basket

	if basket bounds 0 {
		echo drop(basket, 0, 0)
	}
	echo basket
}
Joining, splitting and finding
proc main(): void {
	fruit := ["pear", "apple", "kiwi"]
	line := join(fruit, ", ")
	echo split(line, ", ")

	if indexOf(fruit, "apple") is Result.Ok(i) {
		echo fruit[i]
	}
}
Walking a vector
func isLong(fruit: Str): Bool {
	return len(fruit) > 4
}

func evenHalf(n: Int): Result<Int, Bool> {
	if n % 2 == 0 { return Result.Ok(n / 2) }
	return Result.Error(false)
}

proc main(): void {
	fruit := ["pear", "apple", "kiwi"]
	echo map(fruit, isLong)
	echo filter(fruit, isLong)
	echo filterMap([1, 2, 3, 4], evenHalf)
	echo sort(fruit)
}
Tables
proc main(): void {
	table := [["name", "role"], ["ada", "engineer"], ["grace", "admiral"]]
	echo row(table, "ada")
	echo column(table, "role")
}
These are always in scope, with no import needed. len counts a vector's elements or a Str's characters; bytes counts a vector's backing storage or a Str's UTF-8 bytes. append and prepend grow a mutable dynamic vector at either end; drop removes a range and hands back exactly what it removed. join concatenates a vector of Str with a separator between each pair, and split is its inverse. indexOf finds the first equal element and, unlike a bare -1, an Ok index is always safe to use unguarded. row and column pull a Table's row or column by its first cell or its top cell. map, filter and filterMap each walk a vector with a func — map transforms every element, filter keeps the ones a predicate says yes to, filterMap does both in one pass — and sort puts a vector in order, in place when it stands alone as a statement over a mut vector.A declaration of your own — a func, proc, query or type sharing one of these names — wins over the builtin at every bare call, which stays reachable as hive.<name>.