9.3 Listables
A listable value can be converted to a list using a to_list operation such as Listable.to_list. Lists are themselves listable, where List.to_list just returns the same list. Mutable lists, pair lists, arrays, listable ranges, and instances of classes that implement Listable are also listable.
List-splicing contexts generally allow listables, including in the construction of a list. As a result, if expr produces a listable, another way to convert it to a list is [& expr], while PairList[& expr] would convert it to a pair list.
Typically, a listable object is also sequence and directly indexable, but listable does not imply indexable or working as a sequence.
The interface has a single abstract method:
to_list() —
produces a list with the same elements as the object in the same order.
> Listable.to_list([1, 2, 3])
[1, 2, 3]
> Listable.to_list(PairList[1, 2, 3])
[1, 2, 3]
> Listable.to_list(Array(1, 2, 3))
[1, 2, 3]
annotation | |
> def sizes :: Listable.to_list = Array("small", "medium", "large")
> sizes
["small", "medium", "large"]
fun avg(ns :: (Listable.to_list && NonemptyList.of(Number))):