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)

Treats the Any as a 1-item list 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 smart-matches 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 (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.

Examples:

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.