routine first

Documentation for routine first assembled from the following types:

class Any

From Any

(Any) method first

Defined as:

method first(Mu $matcher?:$k:$kv:$p:$end)

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 $matcher*@elems:$k:$kv:$p:$end)
method first(List:D:  Mu $matcher?:$k:$kv:$p:$end)

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.


say (122/742300).first: * > 5;                  # OUTPUT: «42␤» 
say (122/742300).first: * > 5:end;            # OUTPUT: «300␤» 
say ('hello'122/742'world').first: Complex;   # OUTPUT: «Nil␤» 

The optional named parameters :k, :kv, :p provide the same functionality as on slices:

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.

Return both the index and matched element.

Return the index and the matched element as a Pair.


say (122/742300).first: * > 5:k;        # OUTPUT: «2␤» 
say (122/742300).first: * > 5:p;        # OUTPUT: «2 => 42␤» 
say (122/742300).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.