raku

How do I declare a class attribute as a union of class names?

时光怂恿深爱的人放手 提交于 2020-04-06 05:00:06
问题 I'm reading a spreadsheet looking for different structures. When I tried the following using Moose it seems to do what I want. I could create different types of objects, assign this to the found member and dump the Cell instance for review. package Cell { use Moose; use Moose::Util::TypeConstraints; use namespace::autoclean; has 'str_val' => ( is => 'ro', isa => 'Str', required => 1 ); has 'x_id' => ( is => 'ro', isa => 'Str', ); # later required => 1 ); has 'color' => ( is => 'ro', isa =>

How can I timeout a promise in Raku?

限于喜欢 提交于 2020-03-21 11:36:06
问题 I know I can schedule a Promise to be kept in a given amount of time with my $promise = Promise.in($seconds); but how can I schedule it to be broken? Specifically, I'm thinking of a promise that will "timeout", so that it has up to a certain amount of time to be kept or else it will fail. I can do this with another Promise , like so: my $promise = Promise.new; ... Promise.in($seconds).then: { $promise.break }; But this feels a bit ... wasteful. Is there a better way to do this? 回答1: A common

Raku : is there a SUPER fast way to turn an array into a string without the spaces separating the elements? [closed]

∥☆過路亽.° 提交于 2020-02-29 20:12:10
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 hours ago . I need to convert thousands of binary byte strings, each about a megabyte long, into ASC strings. This is what I have been doing, and seems too slow: sub fileToCorrectUTF8Str ($fileName) { # binary file my $finalString = ""; my $fileBuf = slurp($fileName, :bin); for @$fileBuf

Raku rebless and multiple classes

こ雲淡風輕ζ 提交于 2020-02-23 10:06:47
问题 (This is a follow up to: Raku rebless doesn't work with inherited classes anymore) I have tried to come up with a more complex use case, but am unable to get the code to work. The idea is a Person class, with mixin subclasses for Child and Adult. We have a Child object, and change the type to Adult when the age passes 18 year. This one obviously fails, as Adult is a mixin on Parent, and not on Child: class Person { has Int $.age is rw = 0; method happy-birthday { $.age++; # Metamodel:

Native localtime() segfaults

安稳与你 提交于 2020-02-23 09:07:35
问题 I seem to be doing something wrong in this attempt to expose the localtime functionality in Perl 6: use NativeCall; my class TimeStruct is repr<CStruct> { has int32 $!tm_sec; has int32 $!tm_min; has int32 $!tm_hour; has int32 $!tm_mday; has int32 $!tm_mon; has int32 $!tm_year; has int32 $!tm_wday; has int32 $!tm_yday; has int32 $!tm_isdst; has Str $!tm_zone; has long $!tm_gmtoff; } sub localtime(uint32 $epoch --> TimeStruct) is native {*} dd localtime(time); # segfault Running under perl6

Raku rebless doesn't work with inherited classes anymore

ぃ、小莉子 提交于 2020-02-03 04:34:06
问题 The code given in this thread doesn't work anymore: How can I rebless an object in Perl 6? I wrote this piece of code last year, and it worked then. Now it doesn't: class Person { ; } class Woman is Person { ; } my $tom = Person.new; my $lisa = Woman.new; say $tom.^name; # -> Person say $lisa.^name; # -> Woman Metamodel::Primitives.rebless($tom, Woman); # -> New type Woman for Person is not a mixin type The error message doesn't make sense, as it is supposed to work with inherited classes. At

Altering parameters in Data::Printer in Perl6

a 夏天 提交于 2020-02-02 02:15:34
问题 I'm printing data in Perl6 with Data::Printer which is a spectacular package, but I am trying to alter parameters and I am not able to. For example, I want: HG00112 { gained_site { 9:10162 0, 9:10272 var{HG00112}{gained_site}{9:10162}, 9:10326 var{HG00112}{gained_site}{9:10162}, ... }(tied to Perl6::Hash) to look like HG00112 { gained_site { 9:10162 0, 9:10272 0, 9:10326 0, ... }(tied to Perl6::Hash) for easier readability (I don't care about tied to Perl6::Hash specifically) this hash

Recursive regular expression in Perl 6?

笑着哭i 提交于 2020-02-01 01:39:29
问题 I've been trying to figure out how to do a recursive regular expression in Perl 6. For a toy example, a balanced parentheses matcher, which would match ((())()) inside (((((())()) . PCRE example: /\((?R)?\)/ Onigmo example: (?<paren>\(\g<paren>*\)) I thought this would do it: my regex paren { '(' ~ ')' <paren>* } or the simpler my regex paren { '(' <paren>* ')' } but that fails with No such method 'paren' for invocant of type 'Match' in regex paren at ... 回答1: You need to make explicit that

How can I rebless an object in Perl 6?

吃可爱长大的小学妹 提交于 2020-01-24 03:11:05
问题 Another question might be "How do I inherit from builtin types?". I have two questions really, but they are both from the same thing I'm playing with. First, I can make a subset of a type when I want to constrain it further. I do that with MyInt which accepts anything that is an Int . I declare a variable to by MyInt and assign to it, but when I check its name, I get Int instead. So, what's up with that? subset MyInt where * ~~ Int; my MyInt $b = 137; put 'Name: ', $b.^name; # Int, but why

Why are some of my ranges insane?

流过昼夜 提交于 2020-01-14 14:31:48
问题 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('-