len
func len[T](x: T): intlen returns the size of x: an array's elements, a map's entries, a set's members, a string's code points, the bytes of Bytes, or the values a channel buffers now. x may also be a named primitive over string, which it unwraps.
Details
lenof a channel is the number of values buffered at that moment. Another goroutine can change it right after; an unbuffered channel always has length 0.- Any other argument, including a record, a number, or an optional such as
string | null, is a compile error. Check fornullfirst.
Basic examples
Count the elements of an array and the code points of a string:
len([10, 20, 30]) // 3
len("hello") // 5Count the entries of a map:
len(Map{ "apples": 3, "pears": 0 }) // 2Scope
An empty value has length 0:
let none: Array[int] = []
let empty: Map[string, int] = Map{}
len(none) // 0
len("") // 0
len(empty) // 0A set counts its members, so duplicates count once:
len(Set.of([1, 2, 2, 3])) // 3A string counts code points, and Bytes counts bytes:
len("héllo") // 5
len("héllo".toBytes()) // 6
len(Bytes.fromArray([0, 255])) // 2A named primitive over string is unwrapped:
type UserId = string
len(UserId("u-42")) // 4A union works when len takes every member:
type Text = string | Array[string]
func size(t: Text): int {
len(t)
}
size("abc") // 3
size(["a", "b"]) // 2A channel reports the values it buffers now:
let c: Channel[int] = Channel.new(3)
c.send(1)
c.send(2)
println(len(c))
c.receive()
println(len(c))2
1Pass len as a function where the expected type fixes its parameter:
["a", "bb", "ccc"].map(len) // [1, 2, 3]Applications
Read the last element of an array:
let steps = ["plan", "build", "ship"]
steps[len(steps) - 1] // "ship"Sort words by length:
["pear", "fig", "banana"].sortBy(len) // ["fig", "pear", "banana"]Reject a password that is too short:
func strong(password: string): bool {
len(password) >= 8
}
strong("hunter2") // false
strong("correct horse") // trueCount the distinct words of a sentence:
let words = "the cat saw the dog".split(" ")
len(words) // 5
len(Set.of(words)) // 4Properties and relations
len agrees with the namespace function of each kind:
let xs = [3, 1, 4, 1, 5]
let s = "naïve"
assert(len(xs) == Array.length(xs))
assert(len(s) == String.length(s))
assert(len(Set.of(xs)) == Set.size(Set.of(xs)))A set built from an array is never longer than the array, and a string's UTF-8 bytes are never fewer than its code points:
for s in ["", "abc", "héllo", "日本"] {
assert(len(s.toBytes()) >= len(s))
}
let xs = [1, 1, 2]
assert(len(Set.of(xs)) <= len(xs))Joining two strings adds their lengths:
let a = "foo"
let b = "bär"
assert(len(a + b) == len(a) + len(b))Possible issues
len counts code points, so a character built from several code points counts more than once:
len("e\u{301}") // 2
len("e\u{301}".graphemes()) // 1The last valid index is len(xs) - 1; indexing at len(xs) panics:
let xs = ["a", "b"]
xs[len(xs)] // panic: index 2 out of range for an array of length 2len does not take an optional. Check for null first:
let name: string | null = null
let n = len(name)
// error: len needs an array, a map, a set, a string, Bytes, or a channel, got string | nullA record has no length:
type Point = { x: int, y: int }
let n = len(Point{ x: 1, y: 2 })
// error: len needs an array, a map, a set, a string, Bytes, or a channel, got Pointlen is generic, so it needs an expected function type to be a value:
let f = len
// error: len is generic; call it, or give the expected function type that fixes its type parameterslet f: func(string): int = len
f("abc") // 3See also
Array.length, String.length, String.graphemes, Bytes.length, Map.size, Set.size, Channel.length, Array.isEmpty, String.isEmpty
