routine squish
Documentation for routine squish
assembled from the following types:
class Supply
From Supply
(Supply) method squish
method squish(Supply: :, : --> Supply)
Creates a supply that only provides unique values, as defined by the optional :as
and :with
parameters (same as with List.squish).
class Any
From Any
(Any) method squish
Defined as:
multi method squish( :!, : = &[===] )multi method squish( : = &[===] )
Similar to .repeated
, returns the sequence of first elements of contiguous sequences of equal elements, after normalization by the function :as
, if present, and using as an equality operator the :with
argument or ===
by default.
"aabbccddaa".comb.squish.say; # OUTPUT: «(a b c d a)»"aABbccdDaa".comb.squish( :as() ).say; # OUTPUT: «(a B c d a)»(3+2i,3+3i,4+0i).squish( as => *.re, with => &[==]).put; #OUTPUT: «3+2i 4+0i»
As shown in the last example, a sequence can contain a single element. See squish
for additional sub
examples.
language documentation Independent routines
From Independent routines
(Independent routines) routine squish
Defined as:
sub squish( +values, |c)
Returns a sequence of values from the invocant/argument list where runs of one or more values are replaced with only the first instance. Like unique
, squish
uses the semantics of the === operator to decide whether two objects are the same. Unlike unique
, this function only removes adjacent duplicates; identical values further apart are still kept. The order of the original list is preserved even as duplicates are removed.
Examples:
say <a a b b b c c>.squish; # OUTPUT: «(a b c)»say <a b b c c b a>.squish; # OUTPUT: «(a b c b a)»
The optional :as
parameter, just like with unique
, allows values to be temporarily transformed before comparison.
The optional :with
parameter is used to set an appropriate comparison operator:
say [42, "42"].squish; # OUTPUT: «(42 42)»# Note that the second item in the result is still Strsay [42, "42"].squish(with => :<eq>); # OUTPUT: «(42)»# The resulting item is Int