raku

Impossible to put a map in sink context

喜夏-厌秋 提交于 2020-05-14 17:35:21
问题 I'm trying to see the way of throwing a map in sink context. In this code class Sunk { has $.titanic; method sink { say "Sinking $!titanic"; } } Sunk.new( :titanic($_) ) for 1..3; (1..3).map: { Sunk.new( :titanic($_) ) }; The for loop effectively sinks all the created, the map does not. Any idea why? This test in roast:https://github.com/perl6/roast/blob/b9bfe1844db25f65a4aeb351a0107f83689cb5c2/S04-statements/sink.t#L27-L32 is supposed to work as a test for that. And map is effectively in a

Impossible to put a map in sink context

丶灬走出姿态 提交于 2020-05-14 17:35:07
问题 I'm trying to see the way of throwing a map in sink context. In this code class Sunk { has $.titanic; method sink { say "Sinking $!titanic"; } } Sunk.new( :titanic($_) ) for 1..3; (1..3).map: { Sunk.new( :titanic($_) ) }; The for loop effectively sinks all the created, the map does not. Any idea why? This test in roast:https://github.com/perl6/roast/blob/b9bfe1844db25f65a4aeb351a0107f83689cb5c2/S04-statements/sink.t#L27-L32 is supposed to work as a test for that. And map is effectively in a

How to override the NQPMatch.Str function

爷,独闯天下 提交于 2020-04-13 03:55:31
问题 ... Or how to change $<sigil>.Str value from token sigil { ... } idependently from the matched text . Yes I'm asking how to cheat grammars above (i.e. calling) me. I am trying to write a Slang for Raku without sigil. So I want the nogil token, matching anything <?> to return NqpMatch that stringifies: $<sigil>.Str to '$'. Currently, my token sigil look like that token sigil { | <[$@%&]> | <nogil> { say "Nogil returned: ", lk($/, 'nogil').Str; # Here It should print "$" } } token nogil-proxy {

Check if a list is empty (Raku)

血红的双手。 提交于 2020-04-12 20:23:46
问题 FAQ: In Raku how to check if a list is empty ? Are there more idiomatic ways than: my @l = (); say @l.elems == 0; say @l == (); say @l.Bool; The doc on list recommends smartmatching say $l ~~ (); Do you know other ways ? Can you explain why () === () is wrong even if "" === "" is right: I'm not clear on that. 回答1: Of those suggested: say @l.elems == 0; This is a good one to avoid, because it forces evaluation of all elements in a lazy list (which may lead to an exception if there are

Check if a string contains a substring. Additionally, get index and number of match (Raku)

天大地大妈咪最大 提交于 2020-04-12 20:22:55
问题 FAQ: In Raku, how to check if a String contains a substring ? Where and how many times ? I would like 3 functions such as: xxx-bool("az and az and az again", "az"); # True xxx-num("az and az and az again", "az"); # 3 xxx-list("az and az and az again", "az"); # (0 7 14) PS: Routines index and rindex are pretty cool but only get one match. Related Links: Answer with Regex Answer on List (Array) Contiguous Sequences on List Answer on Hash (Dictionary) 回答1: To check if it contains, use .contains

Check if a string contains a substring. Additionally, get index and number of match (Raku)

余生颓废 提交于 2020-04-12 20:22:28
问题 FAQ: In Raku, how to check if a String contains a substring ? Where and how many times ? I would like 3 functions such as: xxx-bool("az and az and az again", "az"); # True xxx-num("az and az and az again", "az"); # 3 xxx-list("az and az and az again", "az"); # (0 7 14) PS: Routines index and rindex are pretty cool but only get one match. Related Links: Answer with Regex Answer on List (Array) Contiguous Sequences on List Answer on Hash (Dictionary) 回答1: To check if it contains, use .contains

Is it possible to terminate a promise's code block from another promise?

♀尐吖头ヾ 提交于 2020-04-12 09:31:11
问题 I wrote this test program: await Promise.anyof( Promise.allof((^5).map: {start { sleep 10; say "done $_" } }), Promise.in(5).then: { say 'ouch' } ); sleep 10; When the second promise times out it prints 'ouch' and the await exits, but the first promise's code block is still running. After five more seconds its five processes end and print 'done': $ ./test1.p6 ouch done 0 done 1 done 2 done 3 done 4 I tried to terminate the first promise assigning it to a variable and then calling the .break

perl6 What is a quick way to de-select array or list elements?

你说的曾经没有我的故事 提交于 2020-04-12 09:03:09
问题 To select multiple elements from an array in perl6, it is easy: just use a list of indices: > my @a = < a b c d e f g >; > @a[ 1,3,5 ] (b d f) But to de-select those elements, I had to use Set: > say @a[ (@a.keys.Set (-) (1,3,5)).keys.sort ] (a c e g) I am wondering if there is an easier way because the arrays I use are often quite large? 回答1: sub infix:<not-at> ($elems, @not-ats) { my $at = 0; flat gather for @not-ats -> $not-at { when $at < $not-at { take $at++ xx $not-at - $at } NEXT { $at

Perl 6: what's the best way to check if an element is in a list?

邮差的信 提交于 2020-04-10 08:00:10
问题 Let's say I have a large array, @stuff , and a $thing , and I want to know if $thing is in @stuff . What's the best way to do that in Perl 6? And with “best” I mean: idiomatic, readable, performant; not necessarily in that order. There are actually two separate cases. One is where you have to do a lot of checks for different $thing s, the other is where you only do this once or a few times. Let's look at the first case first. I think I know the (or a) right answer. my $set-of-stuff = set

perl6: Cannot unbox 65536 bit wide bigint into native integer

萝らか妹 提交于 2020-04-07 03:00:26
问题 I try some examples from Rosettacode and encounter an issue with the provided Ackermann example: When running it "unmodified" (I replaced the utf-8 variable names by latin-1 ones), I get (similar, but now copyable): $ perl6 t/ackermann.p6 65533 19729 digits starting with 20035299304068464649790723515602557504478254755697... Cannot unbox 65536 bit wide bigint into native integer in sub A at t/ackermann.p6 line 3 in sub A at t/ackermann.p6 line 11 in sub A at t/ackermann.p6 line 3 in block