Is there an infix version for a routine call in perl 6?

Let's say I want to apply an array of functions to an array of objects. Something like that:

my $a = sub ($) { $_ * 2 }; my $b = sub ($) { $_ / 2 }; my @funcs = ($a, $b); my @ops = @funcs.roll(4); 

I could do

 say ^10 ».&» @ops; 

but .& is a postfix operator ; However, for →. which can be used for one function. Using

 say ^10 Z. @ops; 

s , which is actually an infix operator for calling a method, gives an error error for the concatenation operator. I don’t think I have another option. Any ideas?

+5
source share
2 answers

You can use the &infix:« o » / &infix:« ∘ » operator in combination with the lambda / closure factory.

 sub lambda-factory ( +in ) { in.map: -> \_ { ->{_} } } my @funcs = ( * * 2, * / 2 ); (@funcs X∘ lambda-factory ^10)».() # (0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5) (@funcs «∘» lambda-factory ^10)».() # [0, 0.5, 4, 1.5, 8, 2.5, 12, 3.5, 16, 4.5] ( (|@funcs xx *) Z∘ lambda-factory ^10 )».() # (0, 0.5, 4, 1.5, 8, 2.5, 12, 3.5, 16, 4.5) 
+6
source

If you have defined values, you can use the infix andthen operator .

  say (^10 »andthen» (* * 2, * / 2).roll(4)) 
+3
source

Source: https://habr.com/ru/post/1276216/


All Articles