Can Perl6 cmp two strings case insensitively?

  • A+
Category:Languages

I am doing a sort and would like to control the cmp of alpha values to be case insensitive (viz. https://perl6.org/archive/rfc/143.html).

Is there some :i adverb for this, perhaps?

 


Perl 6 doesn't currently have that as an option, but it is a very mutable language so let's add it.

Since the existing proto doesn't allow named values we have to add a new one, or write an only sub.
(That is you can just use the multi below except with an optional only instead.)

This only applies lexically, so if you write this you may want to mark the proto/only sub as being exportable depending on what you are doing.

proto sub infix:<leg> ( /a, /b, *% ){*}  multi sub infix:<leg> ( /a, /b, :ignore-case(:$i) ){   $i    ?? &CORE::infix:<leg>( fc(a) , fc(b) )   !! &CORE::infix:<leg>(    a  ,    b  ) } 
say 'a' leg 'A';     # More say 'a' leg 'A' :i;  # Same say 'a' leg 'A' :!i; # More  say 'a' leg 'A' :ignore-case; # Same 

Note that :$i is short for :i( $i ) so the two named parameters could have been written as:
:ignore-case( :i( $i ) )

Also I used the sub form of fc() rather than the method form .fc because it allows the native form of strings to be used without causing autoboxing.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: