Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mix syntax for Boolean elements

Tags:

raku

If I try to declare a Mix with Boolean components:

my $mix= (True => 0.3, False => 0.7).Mix;
dd $mix; # OUTPUT: «Mix $mix = ("True"=>0.3,"False"=>0.7).Mix␤»

They use Pair syntax, which quotes automatically those bare identifiers. In order to avoid that, you either have to define the Pairs explicitly via Pair.new, or else use the fully qualified name.

my $mix= (Bool::True => 0.3, Bool::False => 0.7).Mix;

Is there any other way of doing that? A simpler way maybe?

like image 395
jjmerelo Avatar asked Jan 31 '19 19:01

jjmerelo


Video Answer


2 Answers

You can use anything that isn't seen as a bare-word.

Fully qualified names work.

Bool::True => 1

The reason they work is bare-words don't have :: in them.
So you can just prepend :: as well.

::True => 1

You can use ::(…)

::(True) => 1

::('True') => 1
::< True > => 1

You can also use () around True.

(True) => 1

You could declare it backwards and use .antipair

( 1 => True ).antipair

( :foo ).antipair # (Bool::True) => 'foo'

If you don't mind getting a sequence you can use .invert, or .antipairs

# Seq.new( Bool::True => 1 )
( 1 => True ).invert 
( 1 => True ).antipairs

# Seq.new( Bool::True => 1, Bool::False => 2 )
( 1 => True, 2 => False ).invert
( 1 => True, 2 => False ).antipairs

If True was a subroutine instead of a term, you could append ()

sub True ( --> True ){}

True() => 1

Then there is using Pair.new.

Pair.new( True, 1 )
like image 198
Brad Gilbert Avatar answered Oct 10 '22 21:10

Brad Gilbert


Using parens as in (True) => 0.3 or the null pseudo-package as in ::True => 0.3 would be another option.

like image 27
Christoph Avatar answered Oct 10 '22 23:10

Christoph