perl6

Which context confuses this Perl 6 zip operator?

无人久伴 提交于 2019-12-05 02:03:33
Consider this program where I create a hash. I want to then change two values in it: my $hash = %( wallet => 100, gave => 0, received => 0, ); for ^1 { $hash<wallet gave> Z+= <-1 1> }; dd $hash; Like this, the last line of for doesn't do anything and there is no warning. The hash is unchanged: Hash $hash = ${:gave(0), :received(0), :wallet(100)} Adding another statement changes the behavior: my $hash = %( wallet => 100, gave => 0, received => 0, ); for ^1 { $hash<wallet gave> Z+= <-1 1>; True }; dd $hash; Now the inplace edit does its thing, but there's a warning (although I dispute "useless"

how do I create a stand-alone executable with perl 6?

末鹿安然 提交于 2019-12-05 01:37:08
The OLD Perl 6 faq said: "Rakudo, a Perl 6 compiler based on Parrot, allows compilation to bytecode, and a small wrapper exists that can pack up a bytecode file and parrot into a single executable." So, it was possible to create a stand-alone executable, but I can not find any docs explaining how to go about this, or if it's still possible. So, I turn to you. What is the appropriate set of incantations required to convert Perl 6 code into a stand-alone executable that will work on a system that does not have Perl 6 installed. This is not possible with current Rakudo on MoarVM. There's still

How does a Perl 6 object find a multi method that might be in a parent class or role?

北慕城南 提交于 2019-12-05 01:12:39
Consider this example where a subclass has a multi method with no signature and one with a slurpy parameter: class Foo { multi method do-it { put "Default" } multi method do-it ( Int $n ) { put "Int method" } multi method do-it ( Str $s ) { put "Str method" } multi method do-it ( Rat $r ) { put "Rat method" } } class Bar is Foo { multi method do-it { put "Bar method" } multi method do-it (*@a) { put "Bar slurpy method" } } Foo.new.do-it: 1; Foo.new.do-it: 'Perl 6'; Foo.new.do-it: <1/137>; Foo.new.do-it; put '-' x 10; Bar.new.do-it: 1; Bar.new.do-it: 'Perl 6'; Bar.new.do-it: <1/137>; Bar.new.do

What does duckmap really do?

点点圈 提交于 2019-12-05 00:55:43
From the documentation duckmap will apply &block on each element and return a new list with defined return values of the block. For undefined return values, duckmap will try to descend into the element if that element implements Iterable . But then: my $list = [[1,2,3],[[4,5],6,7]]; say $list.deepmap( *² ); # [[1 4 9] [[16 25] 36 49]] say $list.duckmap( *² ); # [9 9] deepmap behaves pretty much as expected, but I can't really make any sense of what duckmap is doing. This question is related to this issue in perl6/doc . It can be solved with "They couldn't be more different", but I would like

How does Perl 6's multi dispatch decide which routine to use?

别说谁变了你拦得住时间么 提交于 2019-12-04 11:33:19
Consider this program where I construct an Array in the argument list. Although there's a signature that accepts an Array, this calls the one that accepts a List: foo( [ 1, 2, 3 ] ); multi foo ( Array @array ) { put "Called Array @ version" } multi foo ( Array $array ) { put "Called Array \$ version" } multi foo ( List $list ) { put "Called List version" } multi foo ( Range $range ) { put "Called Range version" } I get the output from an unexpected routine: Called Array $ version If I uncomment that other signature, that one is called: Called List version Why doesn't it call the ( Array @array

How many ways are there to describe the Fibonacci sequence in Perl 6?

眉间皱痕 提交于 2019-12-04 07:52:29
问题 I've been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence. I will start this off with the three from masak's journal: my @fibs := (0, 1, -> $a, $b { $a + $b } ... *); my @fibs := (0, 1, { $^a + $^b } ... *); my @fibs := (0, 1, *+* ... *); I was thinking something like this would also work, but I think I have the syntax wrong: my @fibs := (0, 1, (@fibs Z+ @fibs[1..*])); Something there is

pointer to constructor to a class in perl6

拥有回忆 提交于 2019-12-04 03:19:11
问题 I am trying to write some classes with Perl 6 just for testing out Perl 6 classes and methods. Here is the code: class human1 { method fn1() { print "#from human1.fn1\n"; } } class human2 { method fn1() { print "#from human2.fn1\n"; } } my $a = human1.new(); my $b = human2.new(); $a.fn1(); $b.fn1(); print "now trying more complex stuff\n"; my $hum1_const = &human1.new; my $hum2_const = &human2.new; my $c = $hum2_const(); $c.fn1(); Essentially I want to be able to use either the human1

What happens when different thread schedulers are used in the same react block?

末鹿安然 提交于 2019-12-04 01:39:59
This is a follow up question to is whenever signal() in react block order dependent? . The following code using the default scheduler $*SCHEDULER lets the user exit immediately by pressing CTRL-C in the following event loop: use v6; my %scheduler; my $use-default-scheduler = True; if $use-default-scheduler { %scheduler = scheduler => $*SCHEDULER; } react { whenever signal(SIGINT, |%scheduler) { say "Got signal"; exit; } whenever Supply.from-list($*IN.lines, |%scheduler) { say "Got line"; exit if $++ == 1 ; } } I am interested in what happens if I use two different thread schedulers in the same

How do you add a method to an existing class in Perl 6?

自古美人都是妖i 提交于 2019-12-03 23:40:00
The Int class has a method is_prime , so I figured, just for giggles, I'd like to add some other methods to Int for some of my hobby projects that do number theory stuff. I thought I could do something like this: class Int { method is-even (Int:D $number ) returns Bool:D { return False if $number % 2; return True; } } say 137.is-even; But that doesn't work: ===SORRY!=== P6opaque: must compose before allocating I don't know if this means that I can't do that or that I'm doing it incorrectly. I could easily make a new class that inherits from Int , but that's not what I'm interested in: class

How does one write custom accessor methods in Perl6?

随声附和 提交于 2019-12-03 23:17:26
How does one write custom accessor methods in Perl6? If I have this class: class Wizard { has Int $.mana is rw; } I can do this: my Wizard $gandalf .= new; $gandalf.mana = 150; Let's say I want to add a little check to a setter in my Perl6 class without giving up the $gandalf.mana = 150; notation (in other words, I don't want to write this: $gandalf.setMana(150); ). The program should die, if it tries to set a negative mana. How do I do this? The Perl6 documentation just mentions it is possible to write custom accessors, but does not say how. You can get the same accessor interface that saying