14 Nov 2024 |
antononcube |
So, you have/had to use |$_.comb . |
21:40 |
|
dwinkley |
of course gist.github.com/dwinkley2/ba2f74aa...4c5cea9d99 |
|
|
librasteve |
tx! |
|
|
|
@dwinkley I am struggling a bit to understand what you are aiming to do - please can you provide an example result maybe? |
21:47 |
|
dwinkley |
I want to execute every predefined 'replace' on $_ |
21:49 |
|
|
And preferably without mutation |
|
|
|
You can ignore the check_str parts |
21:50 |
|
librasteve |
oh, ok |
|
|
|
i think you need .trans just chacking... |
22:01 |
|
|
my %replace2 = %replace.map( { [.key] => [.value] } ); say @inp>>.trans(%replace2); |
22:24 |
|
|
this does the job - it seems that .trans needs the key and value of Pairs to be lists to do multicharacter insertions docs.raku.org/routine/trans#(Str)_method_trans |
22:26 |
|
|
for @inp -> $inp is rw { for %replace { $inp .= subst( .key, .value ); } say $inp } |
22:56 |
|
|
this works too ... best I can come up with at 11pm ... must sleep |
|
|
15 Nov 2024 |
dwinkley |
Tysm |
05:46 |
|
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 } } |
|
|
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 |
|
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 |
|
.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 |
|
18 Nov 2024 |
lizmat |
and yet another Rakudo Weekly News hits the Net: rakudoweekly.blog/2024/11/18/2024-...butterfly/ |
17:07 |
|
20 Nov 2024 |
renormalist |
in context of grammars, just to make sure: regex/token/rule only *behave* differently (like token doesn't backtrack), but all can contain the full syntax of complex regexes/tokens/rules, right? |
17:28 |
|
|
What nags my brain is the impact of such a behaviour in the overall picture. Like a token 'dummy' which does not backtrack but contains "foo"|"bar" as part of another 'regex' and partially succeeds with "foo", will that *regex* backtrack and potentially try the token 'dummy' with "bar"? |
17:35 |
|
librasteve |
my freind ChatGPT says this chatgpt.com/share/673e1fe7-2354-80...8ce6654164 (which is a lot better answer than I can do ... but it may be hallucinating) |
17:44 |
|
renormalist |
librasteve_: if this sentence is true: "token: |
17:51 |
|
|
"Does not backtrack internally" |
17:52 |
|
|
then the "internally" is probably my answer. |
|
|
|
which it what it explains in 3. |
|
|
|
renormalist is a bit surprised by the first usefule complex answer from a ChatGPT - if it doesn't tell rubbish, which I hope. |
17:55 |
|
librasteve |
yeah - probably needs testing --- I was surprised at how good the answer was also --- my theory is that raku is underdocumented on the web in general so it probbaly only had one relevant source text and didnt try to weave together a bunch of similar but different texts |
17:56 |
|
renormalist |
That's my explanation, too. |
17:58 |
|
|
so thanks, this helps me further. |
17:59 |
|
antononcube |
With relevant prompts, LLMs know Raku very well. |
18:59 |
|
21 Nov 2024 |
todd2 |
how do I ask zef to tell me what an installed package dependencies are? |
03:19 |
|