03:37
Heptite joined
05:45
teatwo left
|
|||
dwinkley | Tysm | 05:46 | |
06:11
Heptite left
06:33
Heptite joined
|
|||
librasteve | here is a more functional way that I think is more in like with your original question | 08:55 | |
say gather { @inp.map: -> $inp is rw { %replace.map( { $inp .= subst(.key, .value) } ); take $inp } } | |||
09:12
teatime joined
|
|||
nahita3882 | as for the root cause of the error, here is a way to check what's going on Cannot resolve caller reduce(Seq:D: Block:D, Str:D) | 14:04 | |
as for the root cause of the error, here is a way to check what's going on Cannot resolve caller reduce(Seq:D: Block:D, Str:D) Here, the : after the first argument's type (Seq:D) means this was actually a method call, which in fact it was on .pairs of a Hash. In methods, the "self" is delimited by a succeeding : to separate itself from the following arguments. So, you called .reduce on a Seq:D (by the | 14:51 | ||
way, the :Ds mean these are defined objects, not undefined (type) objects; Raku calls them "type smileys" :D), with 2 arguments: a Block of code and a String. The error says this was not a fine way of calling and lists what it considers okay. We can check the documentation to see what's what more clearly. To this end, I go to the documentation website, type "reduce" and choose the (top) "Composite" one (where each | |||
"reduce" candidates possibly from different types, be it methods or free form subroutines, are compiled together). Now I arrive at this page. Conveniently, on the left is a Table of Contents bar that shows which Types had to do with "reduce". Recalling that we called the method form on a Seq, we look for Seq. However, it has only List, Supply and Any. Then how did it work?! Well, every Sequence is also an Any, so by | |||
the virtue of inheritance (graph here), that's what's tried to be called. So, we jump there. It lists 3 candidates multi method reduce(Any:U: & --> Nil) multi method reduce(Any:D: &with) multi reduce (&with, +list) | |||
We called a method, so the first two are of interest to us. Interestingly, those two's signatures are (almost) exactly what the error reported! The only difference is error also reports *%_, but that's an unimportant detail because every method by default has that (docs, on why and more); it essentially slurps all the extra named arguments to ease inheritance calls. Okay, we had a đ Sequence, so it | |||
was an Any:D, i.e., we conclude that the 2nd listed method is what we can use to call .reduce on Seq:Ds: you can only pass a binary Block, not further arguments including the "initial" value in your attempt unfortunately. To fix this to adhere to this method's calling way, you are essentially expected to put the initial value before the main Seq you got, then call reduce with the Block you have. Then we have: | |||
($_, |%replace.pairs).reduce(-> $acc, $elt { ... } ) where we make a List where the first element is the initial value, the rest is the pairs you got; | prefix operator says "slip these into the list", so you have 1 + N elements instead of 1 + 1. That would conclude the "workaround", except it doesn't :p Hashes, when iterated, yield the pairs anyway, so you can remove the .pairs call, i.e., ($_, | 14:52 | ||
|%replace).reduce(...) would work the same. As an aside, we are now calling the .reduce method on Lists, whose signatures are listed on the same page linked above, but it's actually the same with Any's signatures. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Lastly, an arguable disadvantage of this slipping to make a new list is a little bit time consuming -- it would be nice if we didn't have to go through the %replace's pairs | |||
twice in the process (one for slipping, one for the actual reduction operation). Here arises my shameless promotion via the third party Iter::Able module. It has 2 ways to address this that allows for only 1 pass over the main data: use Iter::Able <chain insert-at>; # Chain the initial value with the Pairs, then reduce chain([$_], %replace).reduce(...) # or Insert the initial in front, then reduce | |||
%replace.pairs.&insert-at(0 => $_).reduce(...) # need .pairs as index-based insertion won't work on Hashes These work lazily, i.e., only yield data when asked to, so we "reduce" the number of passes over %replace from 2 to 1 :p | |||
antononcube | đŽ | 15:11 | |
17:20
teatime left,
teatime joined
|
|||
dwinkley | wow | 17:27 | |
just wow | |||
thanks so much for the comprehensive breakdown | |||
ab5tract | yeah seriously awesome nahita3882. we should get that in the docs somehow/somewhere! | 17:39 | |
20:41
Heptite left
20:43
Heptite joined
|
|||
.ohnowendigo | I have a file where a subset matches a grammar. But it's not from the beginning, so subparse won't work I think. Is there a way to do this with grammars or should I just use a regex? What if I want the grammar to match three different things in the text? | 22:07 | |
Wait, does subparse accept :g? | |||
antononcube | I made âText::SubParsersâ for similar use cases. | 22:49 | |
What youâd describe happens often when using LLMs. | 22:50 | ||
But I think will be better off using match(/.../, :g) and / or the adverb :ex. | 22:56 | ||
For example, see the section "FEN sub-parser" here: github.com/antononcube/RakuForPred...sona.ipynb | 23:01 | ||
Also, you can use something like : rx/ $<txt>=(.*) <?{ My::Grammar.parse($<txt>.Str).defined }> / | 23:07 |