perl6

“Cannot assign to immutable value” when trying to assign to a string + role

瘦欲@ 提交于 2019-12-03 18:10:09
问题 Starting with the example in the Iterable doc page role DNA does Iterable { method iterator(){ self.comb.iterator } }; my @a does DNA = 'GAATCC'; .say for @a; # OUTPUT: «G␤A␤A␤T␤C␤C␤» I found it weird it's declared using the @ , so I changed it to the natural way of declaring strings, $ : my $a does DNA = 'GAATCC'; But that fails with a somewhat bewildering "Cannot assign to an immutable value". No need to assign on the spot, so we can do: my $a = 'GAATCC'; $a does DNA; .say for $a; Which

How do I invoke a Java method from perl6

本小妞迷上赌 提交于 2019-12-03 17:37:10
问题 use java::util::zip::CRC32:from<java>; my $crc = CRC32.new(); for 'Hello, Java'.encode('utf-8') { $crc.'method/update/(B)V'($_); } say $crc.getValue(); sadly, this does not work Method 'method/update/(B)V' not found for invocant of class 'java.util.zip.CRC32' This code is available at the following links. It is the only example I've been able to find Rakudo Perl 6 on the JVM (slides) Perl 6 Advent Calendar: Day 03 – Rakudo Perl 6 on the JVM 回答1: Final answer Combining the code cleanups

Does Perl 6 have a built-in tool to make a deep copy of a nested data structure?

坚强是说给别人听的谎言 提交于 2019-12-03 13:34:33
Does Perl 6 have a built-in tool to make a deep copy of a nested data structure? Added example: my %hash_A = ( a => { aa => [ 1, 2, 3, 4, 5 ], bb => { aaa => 1, bbb => 2 }, }, ); my %hash_B = %hash_A; #my %hash_B = %hash_A.clone; # same result %hash_B<a><aa>[2] = 735; say %hash_A<a><aa>[2]; # says "735" but would like get "3" my %A = ( a => { aa => [ 1, 2, 3, 4, 5 ], bb => { aaa => 1, bbb => 2 }, }, ); my %B = %A.deepmap(-> $c is copy {$c}); # make sure we get a new container instead of cloning the value dd %A; dd %B; %B<a><aa>[2] = 735; dd %A; dd %B; Use .clone and .deepmap to request a copy

How do I invoke a Java method from perl6

放肆的年华 提交于 2019-12-03 07:41:59
use java::util::zip::CRC32:from<java>; my $crc = CRC32.new(); for 'Hello, Java'.encode('utf-8') { $crc.'method/update/(B)V'($_); } say $crc.getValue(); sadly, this does not work Method 'method/update/(B)V' not found for invocant of class 'java.util.zip.CRC32' This code is available at the following links. It is the only example I've been able to find Rakudo Perl 6 on the JVM (slides) Perl 6 Advent Calendar: Day 03 – Rakudo Perl 6 on the JVM Final answer Combining the code cleanups explained in the Your answer cleaned up section below with Pepe Schwarz's improvements mentioned in the

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

馋奶兔 提交于 2019-12-02 18:13:20
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 eager (the slice?) and causes Rakudo to enter an infinite loop. It's a translation of the Haskell

How does Parrot compare to other virtual machines?

时间秒杀一切 提交于 2019-12-02 18:01:33
Parrot is the virtual machine originally designed for Perl 6. What technical capabilities does the Parrot VM offer that competing virtual machines such as the Java Virtual Machine (JVM) / Hotspot VM and Common Language Runtime (CLR) lack? Schwern The following answer was written in 2009. See also this 2015 update by raiph . To expand on @Reed and point out some highlights, Parrot's opcodes are at a far higher level than most virtual machines. For example, while most machines store integers and floats, the basic registers are integers, numbers, strings and Parrot Magic Cookies (PMCs). Just

What's the deal with all the different Perl 6 equality operators? (==, ===, eq, eqv, ~~, =:=, …)

爷,独闯天下 提交于 2019-12-02 17:54:05
Perl 6 seems to have an explosion of equality operators. What is =:= ? What's the difference between leg and cmp ? Or eqv and === ? Does anyone have a good summary? =:= tests if two containers (variables or items of arrays or hashes) are aliased, ie if one changes, does the other change as well? my $x; my @a = 1, 2, 3; # $x =:= @a[0] is false $x := @a[0]; # now $x == 1, and $x =:= @a[0] is true $x = 4; # now @a is 4, 2, 3 As for the others: === tests if two references point to the same object, and eqv tests if two things are structurally equivalent. So [1, 2, 3] === [1, 2, 3] will be false

How to put a sub inside a regex in Perl 6?

心不动则不痛 提交于 2019-12-01 23:32:56
问题 That's what I'm trying to do. > my sub nplus1($n) {$n +1} > my regex nnplus1 { ^ (\d+) &nplus1($0) $ } > "123" ~~ &nnplus1 P6opaque: no such attribute '$!pos' in type Match... 回答1: The <{...}> construct runs Perl 6 code inside a regex, and evaluates the result as a regex: my sub nplus1($n) {$n +1} my regex nnplus1 { ^ (\d+) <{ nplus1($0) }> $ } say so '23' ~~ &nnplus1; # Output: True say so '22' ~~ &nnplus1; # Output: False 回答2: Keep in mind that regexes are subs. So don't call your matcher a

How do I access the captures within a match?

寵の児 提交于 2019-12-01 22:02:04
I am trying to parse a csv file, and I am trying to access names regex in proto regex in Perl6. It turns out to be Nil. What is the proper way to do it? grammar rsCSV { regex TOP { ( \s* <oneCSV> \s* \, \s* )* } proto regex oneCSV {*} regex oneCSV:sym<noQuote> { <-[\"]>*? } regex oneCSV:sym<quoted> { \" .*? \" } # use non-greedy match } my $input = prompt("Enter csv line: "); my $m1 = rsCSV.parse($input); say "==========================="; say $m1; say "==========================="; say "1 " ~ $m1<oneCSV><quoted>; # this fails; it is "Nil" say "2 " ~ $m1[0]; say "3 " ~ $m1[0][2]; Thank you

How to put a sub inside a regex in Perl 6?

可紊 提交于 2019-12-01 20:19:13
That's what I'm trying to do. > my sub nplus1($n) {$n +1} > my regex nnplus1 { ^ (\d+) &nplus1($0) $ } > "123" ~~ &nnplus1 P6opaque: no such attribute '$!pos' in type Match... The <{...}> construct runs Perl 6 code inside a regex, and evaluates the result as a regex: my sub nplus1($n) {$n +1} my regex nnplus1 { ^ (\d+) <{ nplus1($0) }> $ } say so '23' ~~ &nnplus1; # Output: True say so '22' ~~ &nnplus1; # Output: False Keep in mind that regexes are subs. So don't call your matcher a sub —be more specific and call it a regex . Yes, you can pass arguments to regex / token / rule . It's really