Home / Docs / Standard library / Built-in methods

len

Release v0.4.0Edition 0.1 and later

A built-in method, resolved by the type of its receiver (spec/14 §2). Edition 0.1 and later.

Edition 0.1

una
t.len -> Int
  where t: Text
xs.len -> Int
  where xs: List[T]

Built in on Text and List and, from edition 0.2 on, on Map (spec/14 §2, spec/21 §2). On a Text it counts Unicode scalar values (spec/14 §1); a map's is never negative (spec/21 §2). From edition 0.5 on, a Stack, a Queue, a Set, a HashMap, a HashSet and a PriorityQueue count their items, and a BitSet its members, in O(1) (spec/28 §5, §6, §7, §8, §9).

Edition 0.2 and later

una
t.len -> Int
  where t: Text
xs.len -> Int
  where xs: List[T]
m.len -> Int
  where m: Map[K, V]

Built in on Text and List and, from edition 0.2 on, on Map (spec/14 §2, spec/21 §2). On a Text it counts Unicode scalar values (spec/14 §1); a map's is never negative (spec/21 §2). From edition 0.5 on, a Stack, a Queue, a Set, a HashMap, a HashSet and a PriorityQueue count their items, and a BitSet its members, in O(1) (spec/28 §5, §6, §7, §8, §9).

Edition 0.5 preview

una
t.len -> Int
  where t: Text
xs.len -> Int
  where xs: List[T]
m.len -> Int
  where m: Map[K, V]
s.len -> Int
  where s: Set[T]
m.len -> Int
  where m: HashMap[K, V]
s.len -> Int
  where s: HashSet[T]
b.len -> Int
  where b: BitSet

Built in on Text and List and, from edition 0.2 on, on Map (spec/14 §2, spec/21 §2). On a Text it counts Unicode scalar values (spec/14 §1); a map's is never negative (spec/21 §2). From edition 0.5 on, a Stack, a Queue, a Set, a HashMap, a HashSet and a PriorityQueue count their items, and a BitSet its members, in O(1) (spec/28 §5, §6, §7, §8, §9).

In editions 0.5. In edition 0.1: no Map[K, V] receiver, since edition 0.1 has no maps, and no Set[T], HashMap[K, V], HashSet[T] or BitSet receiver: it has none of m.len -> Int, where m: Map[K, V], s.len -> Int, where s: Set[T], where m: HashMap[K, V], where s: HashSet[T], b.len -> Int and where b: BitSet. In edition 0.2, 0.3, 0.4: no Set[T], HashMap[K, V], HashSet[T] or BitSet receiver, since std.collections begins in edition 0.5: it has none of s.len -> Int, where s: Set[T], where m: HashMap[K, V], where s: HashSet[T], b.len -> Int and where b: BitSet.

Receiver

Returns

Int: the length of the receiver, a text, a list, a map, a set, a hash table or a bit set.

See also

is_empty, bytes (edition 0.2 on)

Cost

Time O(1) on a Text (ADR 0070).

Example

src/shop.unauna
module shop

--- The length of a name, of a cart and of a stock map.
pub main() -> Unit
  uses @console: Console[print]
=
  let name = "Café"
  @console.print("{name.len} characters")
  let cart = ["lamp", "desk"]
  @console.print("{cart.len} items")
  let stock = empty_map().with(key: "lamp", value: 3).with(key: "desk", value: 1)
  @console.print("{stock.len} entries")

una run . prints:

Output
4 characters
2 items
2 entries

Specification

Generated from una v0.4.0 (docs/std/builtins.json, which is not part of the specification): built-in methods have no declaration, so una writes these signature lines and tests each against its checker. The specification governs: spec/14 §2.