Richard Hainsworth wrote:
> I dont think the specification regarding 'reverse' has ever changed.

Actually it has changed. If my memory serves me right, .flip is not
older than one or many one and a half years.

The story is rather simple: In Perl we usually have one operator per
operation. For example: eq for string comparison, == for numeric
comparsion. These operators coerce to the type that's necessary to
conduct the actual operation.

reverse() violated that principle, by doing several different operations
depending on the data type. So we split it up into .reverse (list
reversal), .flip (string reversal) and .invert (key/value inversion).

> 'flip' has more uses than just for strings. For example, it flips the 
> key/value in a pair. Hence,
> 
> my %h = <a b c d> Z 1,2,3,4; # a neat way of specifying a hash in terms 
> of two lists
> say (map { .flip }, %h).perl; # the .perl gives you more information 
> about the structure of the result.
> # ("3\tc", "1\ta", "2\tb", "4\td")

The output tells you that .flip coerced to Str, and then reversed the
string. With .invert (which is not yet impemented in Rakudo), you'd get
3 => 't' instead of "3\tc".

Cheers,
Moritz

Reply via email to