raku

Why are some of my ranges insane?

心已入冬 提交于 2020-01-14 14:31:11
问题 I tried parsing a common string depiction of ranges (e.g. 1-9 ) into actual ranges (e.g. 1 .. 9 ), but often got weird results when including two digit numbers. For example, 1-10 results in the single value 1 instead of a list of ten values and 11-20 gave me four values ( 11 10 21 20 ), half of which aren't even in the expected numerical range: put get_range_for('1-9'); put get_range_for('1-10'); put get_range_for('11-20'); sub get_range_for ( $string ) { my ($start, $stop) = $string.split('-

What type are Lists of any type in Perl 6?

我只是一个虾纸丫 提交于 2020-01-14 13:03:29
问题 Consider the following Python code (as an example): a = 5 b = "a" l = [a, b] # -> typing.List[typing.Any] print(l) # [5, "a"] The type of list l is list ; it is not constrained by the types it holds because Python is quite dynamically typed. Contrast this with, say, Go, which is strongly structurally typed: var list []uint8{1, 2, 3, 4, 5, 6} That list can only hold unsigned integers up to 255. It cannot hold any other type. Also Go: var multi interface{"string", []int{9, 5}, rune('5'), [

What type are Lists of any type in Perl 6?

人走茶凉 提交于 2020-01-14 13:00:07
问题 Consider the following Python code (as an example): a = 5 b = "a" l = [a, b] # -> typing.List[typing.Any] print(l) # [5, "a"] The type of list l is list ; it is not constrained by the types it holds because Python is quite dynamically typed. Contrast this with, say, Go, which is strongly structurally typed: var list []uint8{1, 2, 3, 4, 5, 6} That list can only hold unsigned integers up to 255. It cannot hold any other type. Also Go: var multi interface{"string", []int{9, 5}, rune('5'), [

is whenever signal() in react block order dependent?

删除回忆录丶 提交于 2020-01-14 07:33:30
问题 I have a small program which runs until a SIGINT is received or two lines (press enter twice) from stdin are received. The react block logic is: react { whenever signal(SIGINT) { say "Got signal"; exit; } whenever $*IN.lines.Supply { say "Got line"; exit if $++ == 1 ; } } Program will exit on two entered lines as expected. However CTRL-C will not do anything, unless it is followed by a line (enter). If I switch the order of the whenever blocks, the program is interrupted by a SIGINT but doesn

Overloading operators for a class

余生长醉 提交于 2020-01-14 07:24:21
问题 Let's say I have the following class: class A { has $.val; method Str { $!val ~ 'µ' } } # Is this the right way of doing it? multi infix:<~>(A:D $lhs, A:D $rhs) { ('(', $lhs.val, ',', $rhs.val, ')', 'µ').join; } How would I overload an operator (e.g., + ) for a class in the same manner as Str in the previous class? I guess this only works for methods that are invoked on an instance object and using the multi operator-type:<OP>(T $lhs, T $rhs) { } syntax for operators is the right way to go

How can I use “map” inside a “for” loop in Perl 6?

安稳与你 提交于 2020-01-13 08:35:10
问题 I'm trying to use map inside a for loop, but this code is not working. This code should print 1 and 2 three times. But the code below produces no output. It will only partially work if you uncomment the exit line. Any idea why? I tested it using the current Perl 6 version (Rakudo version 2015.12-79-g4bb47d5 built on MoarVM version 2015.12 and also the Windows version). for (1..3) { map { say $_ }, (1..2); # exit; } 回答1: Let's take a look at say (map { say $_ }, 1..2).WHAT; This tells us &map

NativeCall loading a library symbol I don't call

旧巷老猫 提交于 2020-01-13 05:19:09
问题 I have two libraries, I want to call routines in the first library, they then call routines in the second library, but crash because those symbols are undefined. Is it possible to say "load these symbols" from library XX even though I don't want to call them? testlib1.c: #include <stdio.h> void sub2(); void sub1() { printf("Called sub1\n"); sub2(); } testlib2.c: #include <stdio.h> void sub2() { printf("Called sub2\n"); } testit.p6: use NativeCall; sub sub1() is native('testlib1') {} sub sub2(

How can classes be made parametric in Perl 6?

旧街凉风 提交于 2020-01-12 15:46:05
问题 Normally in Perl 6, only roles are allowed to be parametric. Here, we'll be attempting to make classes, a kind (referred to from here on out as a metaobject) that isn't normally allowed to be parametric, parametric. If you try to make a class parametric the naive way, this happens: bastille% perl6 -e 'class Foo[::T] {}' ===SORRY!=== Error while compiling -e Unable to parse class definition at -e:1 ------> class Foo⏏[::T] {} expecting any of: generic role But if you take a look at what

Inheriting private attributes in Perl 6

若如初见. 提交于 2020-01-11 08:16:28
问题 I can't find anything in the docs, but it seems that there is no access in a subclass to its superclass's private variables. Am I right? class A { has $!a; } class B is A { has $.b; method set_a($x) { $!a = $x; } } my $var = B.new(); $var.set_a(5); say $var.a; This gives an error message: Attribute $!a not declared in class B BTW where to read about Classes in the docs? I have only found a rather short section Classes and Objects. 回答1: In Perl 6, an attribute declared in a class is accessible

How do I turn the Perl 5 module Data::Printer's `show_tied` option off when using it in Raku?

青春壹個敷衍的年華 提交于 2020-01-11 05:07:12
问题 I've used the CPAN Perl module Data::Printer (DP) with Perl. It works great. Now I want to use it in Raku code. When I use the :from<Perl5> feature to import it and then run code using it, the annotation (tied to Perl6::Hash) is appended to the display of hashes. 1 As DP's CPAN doc shows, this annotation is controlled by the option show_tied . I want to switch it off (set it to 0 ) instead of its default on (set to 1 ). Here's how I'd do that in Perl: use Data::Printer show_tied => 0; But