perl6

What are callwith and samewith supposed to do?

放肆的年华 提交于 2019-12-07 05:42:00
问题 What are callwith and samewith supposed to do? I thought callwith was supposed to run a subroutine of the same name of the current subroutine, but using the arguments I pass to it. From the docs: callwith calls the next matching candidate with arguments provided by users and returns that candidate's return value. To me it sounds like inside the two-arity MAIN subroutine (i.e. MAIN($a,$b) ) that callwith($x) would call MAIN($x) . But it doesn't: multi MAIN ($a, $b) { my $result = callwith("$a

Find last Friday’s Date in Perl 6?

不羁的心 提交于 2019-12-07 04:46:45
问题 I want to generate a sequence that ends last Friday from Monday to Thursday, and the Friday of the previous week if the sequence starts on Saturday and Sunday. That is, assuming that today is 2018-05-09 , then last Friday is 2018-05-04 , If today is 2018-05-12 , then last Friday is also 2018-05-04 . So I write: (Date.today, *.earlier(:1day) ... ( *.day-of-week==5 && *.week[1]+1==Date.today.week[1] )).tail # Output: 2018-05-06 But the result is 2018-05-06 instead of 2018-05-04 . Then I used a

index() error message incorrect for third parameter?

若如初见. 提交于 2019-12-07 03:09:24
问题 sub count-a { my $word = "banana"; my $count = 0; my $foo; # Source of error: $foo intentionally not given a value. # The value ought to be zero. while True { $foo = index $word, "a", $foo; last unless $foo.defined; $foo++; $count++ } return $count; } say count-a; Is this error message wrong? Cannot resolve caller index(Str: Str, Any); none of these signatures match: (Str:D $: Cool:D $needle, *%_) (Str:D $: Str:D $needle, *%_) (Str:D $: Cool:D $needle, Cool:D $pos, *%_) (Str:D $: Str:D

Should Perl 6 run MAIN if the file is required?

喜欢而已 提交于 2019-12-07 02:53:35
问题 Here's a short Perl 6 program that declare a MAIN subroutine. I should only see output if I execute the program directly: $ cat main.pm6 sub MAIN { say "Called as a program!" } And I see output when I execute the program directly: $ perl6 main.pm6 Called as a program! If I load it as a module, I see no output: $ perl6 -I. -Mmain -e "say 'Hey'" Hey Same if I use it from inside the program, I see no output: $ perl6 -I. -e 'use main' But, if I use require , I get output: $ perl6 -I. -e 'require

Last element of a block thrown in sink context

时间秒杀一切 提交于 2019-12-07 02:13:22
问题 This program my @bitfields; for ^3 -> $i { @bitfields[$i] = Bool.pick xx 3; } my @total = 0 xx 3; for @bitfields -> @row { @total Z+= @row; } say @total; says [0 0 0] . If we add something to the loop, whatever: my @bitfields; for ^3 -> $i { @bitfields[$i] = Bool.pick xx 3; } my @total = 0 xx 3; for @bitfields -> @row { @total Z+= @row; say "foo"; } say @total; It will work correctly. Apparently, the last element of the block is thrown into sink context which in this case means it's simply

Is there research on performance penalties for types/constraints in Perl 6?

早过忘川 提交于 2019-12-06 23:28:50
问题 In contrast with Perl 5, Perl 6 introduced optional typing, as well as constraints, e.g.: # Perl 5 sub mySub { my $probability = $_[0]; # Do stuff with $probability } # Perl 6 - using optional typing and constraints sub mySub(Real $probability where 0 < * < 1) { # Do stuff with $probability } Have there been studies that investigate whether there are performance penalties, and how large are they on different Perl 6 VMs, when using these capabilities? I'm looking for something well designed,

How can I discover all the roles a Perl 6 type does?

耗尽温柔 提交于 2019-12-06 20:24:51
问题 With .does I can check if a type has the role I already know. I'd like to get the list of roles. Inheritance has .^mro but I didn't see anything like that for roles in the meta model stuff. Along with that, given a "type", how can I tell if it was defined as a class or a role? 回答1: .^roles say Rat.^roles; # ((Rational[Int,Int]) (Real) (Numeric)) By default it includes every role, including roles brought in by other roles. To only get the first level use :!transitive Rat.^roles(:!transitive);

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

穿精又带淫゛_ 提交于 2019-12-06 20:18:59
问题 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

Executing Perl 6 code in Rmarkdown

試著忘記壹切 提交于 2019-12-06 19:37:17
问题 I want to write some tutorials on Perl 6. For this I believe Rmarkdown would be of great help. So I am trying to execute Perl 6 code within Rmarkdown document. My Perl 6 executable is in C:\rakudo\bin . So my .Rmd file with example code to accomplish this is as follow: --- title: "Example" output: html_document --- ```{r, engine='perl6', engine.path='C:\\rakudo\\bin'} my $s= "knitr is really good"; say $s; ``` However knitting the above document in Rstudio shows the following without Perl 6

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

六月ゝ 毕业季﹏ 提交于 2019-12-06 05:23: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