routine first
Documentation for routine first assembled from the following types:
class Any
From Any
(Any) method first
Defined as:
method first(Mu ?, :, :, :, :)
Coerces the invocant to a list by applying its .list method and uses List.first on it.
say Any.first; # OUTPUT: «(Any)»
class List
From List
(List) routine first
Defined as:
sub first(Mu , *, :, :, :, :)method first(List: Mu ?, :, :, :, :)
Returns the first item of the list which smartmatches against $matcher, returns Nil when no values match. The optional named parameter :end indicates that the search should be from the end of the list, rather than from the start.
Examples:
say (1, 22/7, 42, 300).first: * > 5; # OUTPUT: «42»say (1, 22/7, 42, 300).first: * > 5, :end; # OUTPUT: «300»say ('hello', 1, 22/7, 42, 'world').first: Complex; # OUTPUT: «Nil»
The optional named parameters :k, :kv, :p provide the same functionality as on slices:
k
Return the index value of the matching element. Index is always counted from the beginning of the list, regardless of whether the :end named parameter is specified or not.
kv
Return both the index and matched element.
p
Return the index and the matched element as a Pair.
Examples:
say (1, 22/7, 42, 300).first: * > 5, :k; # OUTPUT: «2»say (1, 22/7, 42, 300).first: * > 5, :p; # OUTPUT: «2 => 42»say (1, 22/7, 42, 300).first: * > 5, :kv, :end; # OUTPUT: «(3 300)»
In method form, the $matcher can be omitted, in which case the first available item (or last if :end is set) will be returned. See also head and tail methods.