tailgate I have a list(@Bits) of a class I created, BitString::BitString which has the operator ~ overriden. It's been working fine until I tried 00:00
my $result = @Bits.reduce(&infix:<~>)
this only works when 2 BitStrings are in the list. If I have 3, instead of returning a BitString, it returns BitString::BitString<140428016216096>BitString::BitString<140428016216136>BitString::BitString<140428016216176>
However, the code
my $result = BitString.new(value => "" );
for @Bits {
$result ~= $_
gets the expected result no matter how many BitStrings are in the list. What am I doing wrong?
00:03 discord-raku-bot left, discord-raku-bot joined
gfldex What does your custom ~ return? 00:15
tailgate BitString
guifa can confirm. reduce is, for some reason, using GLOBAL::infix:<~> rather than the LEXICAL::infix:<~> that was passed 00:37
gfldex Defining a new proto will help with that but will also hide all the other candidates for the operator in question. 00:38
guifa m: class A { has $.v }; multi sub infix:<~> (A $x, A $y) { say "CUSTOM"; A.new: v => $x.v + $y.v }; my @l = do A.new(v => $_) for ^3; say @l.reduce(&infix:<~>);
camelia A<3419364252112>A<3419364252192>A<3419364252232>
gfldex m: class A { has $.v }; multi sub infix:<~> (A $x, A $y) { say "CUSTOM"; A.new: v => $x.v + $y.v }; my @l = do A.new(v => $_) for ^3; say @l.reduce(&infix:<~>); 00:39
camelia A<4262385801680>A<4262385801760>A<4262385801800>
guifa I'm curious why the global one is passed. If we're passing it, the lexical one should absolutely be visible
(in the reduce subroutine, I mean)
gfldex m: class A { has $.v }; proto sub infix:<~>(|){}multi sub infix:<~> (A $x, A $y) { say "CUSTOM"; A.new: v => $x.v + $y.v }; my @l = do A.new(v => $_) for ^3; say @l.reduce(&infix:<~>);
camelia ===SORRY!=== Error while compiling <tmp>
Strange text after block (missing semicolon or comma?)
at <tmp>:1
------> A { has $.v }; proto sub infix:<~>(|){}⏏multi sub infix:<~> (A $x, A $y) { say "
expecting any of:
gfldex m: class A { has $.v }; proto sub infix:<~>(|){*}; multi sub infix:<~> (A $x, A $y) { say "CUSTOM"; A.new: v => $x.v + $y.v }; my @l = do A.new(v => $_) for ^3; say @l.reduce(&infix:<~>); 00:40
camelia CUSTOM
A.new(v => 3)
gfldex guifa: .reduce is rather elaborate. github.com/rakudo/rakudo/blob/mast....pm6#L1832
guifa aha 00:41
It's trying to be fancy
but the find should probably check whether it's identical or not
gfldex And I believe when it comes to metaops there is some caching involved. 00:42
guifa still wishes multi sub GLOBAL::infix:<~>(Foo, Foo) { … } worked haha 00:46
gfldex @tailgate#1731 You are missing a candidate. see: github.com/rakudo/rakudo/blob/mast....pm6#L1388 00:48
(i hope)
tailgate what is a candidate? 00:50
gfldex You need 2 multi candidates to make metaops and thus .reduce work. See the implementation of Blob. 00:52
guifa m: class A { has $.v }; multi sub infix:<~> (A $x) { A.new: v => 0 }; multi sub infix:<~> (A $x, A $y) { A.new: v => $x.v + $y.v }; my @l = do A.new(v => $_) for ^3; say @l.reduce(&infix:<~>); 01:05
camelia A<3996232047056>A<3996232047136>A<3996232047176>
04:30 frost joined 05:49 frost left 08:34 dakkar joined 09:43 dakkar left 12:41 frost joined 13:10 frost left 13:28 frost joined 13:48 discord-raku-bot left 13:51 discord-raku-bot joined 14:29 frost left 14:45 frost joined 14:55 getimiskon joined
getimiskon Hello. I have encountered a weird issue. In my code I use the MAIN routine with a couple of variables, which will be used as arguments. I declared these variables outside of MAIN. While I have given a value, that value seem to work only inside MAIN, and not in other subroutines in which that value is needed to function. I couldn't find anything about it in the documentation. 15:12
MasterDuke can you post a gist with an example? 15:21
getimiskon of course
do you want the part of the code that has the issue, or the whole script? 15:22
MasterDuke heh, depends on the size
getimiskon it's the whole script has less than 100 LOC 15:23
MasterDuke sure, post it
15:25 frost left
getimiskon gist.github.com/getimiskon/a5e68f6...678bacdd66 15:26
here it is
I have the problem I mentioned when the playVideo subroutine runs. But if I add the line "$resolution=[value]" at the beginning of the subroutine, it seems to work just fine. 15:29
MasterDuke hm 15:31
interesting. because $video_link is correct in playVideo 15:33
getimiskon I know
I'm not sure about what's the problem in this case
MasterDuke so this does appear to be a bug. maybe because MAIN is a bit special something is going wrong 15:38
getimiskon I see
MasterDuke well, it seems to have always been that way gist.github.com/Whateverable/f7bc9...3f0350cce2 15:47
mind creating an issue? github.com/rakudo/rakudo/issues/new 15:48
getimiskon ok 15:52
MasterDuke thanks 16:07
gfldex getimiskon: declaring $resolution in MAIN's signature shadows the global $resolution. playVideo referes to global $resolution. You can solve that with a dynvar or by moving playVideo into MAIN. 19:24
getimiskon I see. It seems to work like that. 19:33
gfldex If you wanna be fancy, you could try `use dynamic-scope <$resolution>`. 19:45
getimiskon I'll keep that in mind. It's still weird why one of the variables didn't have that issue. 19:51
stevied I seem to recall that it's possible to change the value of an argument in the signature. Is this possible or am I misremembering? Can't find any documenation. 20:37
gfldex m: sub subby($a is rw where { $a = 42 } ) { say $a }; subby my $cont = 'answer'; 20:41
Only evil ppl are allowed to do so ofc.
stevied Thanks. i'm trying to subtract one from the argument: 20:45
`Int $begin is rw where { --$begin }`
just get errors, though
gfldex $begin has to be a container supplied by the caller.
stevied oh, ok
gfldex m: sub subby($a is copy where { $a++ } ) { say $a }; subby 41;
unless we copy
This is slightly less evil. 20:46
stevied ah, yes. that worked. 20:47
easier to do it in the body of the sub, though 20:49
one more thing: `@list[--$begin..--$end].uc.list;`
getting a cryptic error on this one
nvm, got it. also determined by how args were passed in 20:51
now things are starting to click a lttle
20:53 discord-raku-bot left 20:54 discord-raku-bot joined 22:37 getimiskon left
how do I convert an Array object to a List object? `@list.list` returns a sequence. same with `@list.flat` 23:05
OK, it's `List` not `list`. 2nd time I've made that mistake 23:07
23:16 Esoren joined
Esoren Howdy.  Is this a good place to ask language questions? 23:17
MasterDuke sure. it's getting a bit late though, since a lot of the people are in europe, so you might not get an answer until tomorrow 23:21
gfldex Depends a bit on the language. 23:22
Esoren Thanks.  I'm logged in through the web chat, so if I close this window I won't see replies after I also go to bed.  So my first question is, where is this chat logged so I can read what I've missed?
MasterDuke fyi, you can read logs of the chat here logs.liz.nl/raku-beginner/2022-01-22.html 23:24
Esoren Thank you!  The other first question is hopefully simple.  Let's say I've got a Str in $x.  What's the idiomatic way of determining if I can convert it to an Int?  Or, a method to try to convert it to an Int that fails peacefully if it's not int'able?  I know one way is to just see if it matches `/\d+/`.  I saw online another way is to check 23:25
if `$x.Int !~~ Failure`.  Both of these ways feel kind of janky to me.
(I don't care about int overflows or even negatives.  The input would be a human-countable whole number, or else an unrelated string.) 23:26
MasterDuke m: my $a = "abc"; if try $a.Int { say "a is Intable" }; my $b = "123"; if try $b.Int { say "b is Intable" }   # here's another way 23:27
camelia b is Intable
Esoren Nice, that seems good and short.  I hadn't come cross that try piece yet.  Thanks, let me give it a shot. 23:28
MasterDuke np 23:29
m: my $b = "0"; if try $b.Int { say "b is Intable" }   # this way does have some caveats though 23:30
camelia ( no output )
MasterDuke m: my $b = "0"; with try $b.Int { say "b is Intable" }   # so maybe this is better
camelia b is Intable
Esoren Interesting.  By coincidence, I don't want to deal with zeros either, so the first way works, but I'll need to read up more about with as well. 23:31
MasterDuke it's very like if, but it tests definedness, not truthiness 23:34
Esoren Okay, cool, I like this approach the best. Okay, second question: I'm composing regexes from (trusted) user input. I've got a pile of strings, and I want to conjoin them with &, as if it looked like `my $pattern = '^ ' ~ @patterns.map({"[$_]"}).join(' & ') ~ ' $'`.  That part works well enough.  The problem is that rx/$pattern/ or
rx/<{$pattern}>/ or rx/($pattern)/ all create a regex that re-evaluates $pattern every time it's run.  When running this on a large file, it's super super slow.  But I found a workaround.  If I use `EVAL "rx/$pattern/"` then it's as fast as if I wrote the regex as a literal.  Not an idea workaround but it works well.  What's the proper way to 23:35
do this?
MasterDuke well, first of all <$pattern> is probably what you want 23:38
but yes, i believe that is still going to re-evaluate it every time 23:39
and regexes/grammars are currently the slowest part of raku. so honestly, you're likely best off using EVAL for now 23:40
Esoren ah, so be it then.  Thanks for the answers.
MasterDuke np 23:41