This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html
Set by lizmat on 8 June 2022.
stevied having some trouble figuring out how to do a search and replace on text in an array. Tried this: 00:14
```
for @subsections[1..*-1] -> $text is rw {
$text ~~ s/^^ '#' \s* /''/;
}
```
throws: `Cannot assign to a readonly variable or a value` 00:16
00:34 Kaiepi left, Kaipei joined 00:47 frost joined
nvm, figured it out 01:37
is there a way to get `say` to return the actual string instead of a boolean? 01:43
ok, I can just use .gist 01:45
06:52 frost left 07:48 dakkar joined 08:24 frost joined
Nemokosch Is there a more idiomatic way to "flat-zip" than simply applying flat after zipping? 09:00
09:04 frost left 09:33 lizmat_ joined 09:35 lizmat left, lizmat_ is now known as lizmat 10:48 frost joined 11:06 frost left 11:13 frost joined 11:25 frost left 11:36 frost joined 11:58 frost left, frost joined 12:17 frost left 12:25 frost joined 13:00 frost left
Util m: say (<a b c> Z <m n o>).flat; 13:09
Util m: say (<a b c> Z <m n o>).flat;
camelia (a m b n c o)
Util m: say flat <a b c> Z <m n o>; 13:10
m: say flat [Z] <a b c>, <m n o>;
<@297037173541175296> : I tried using `Z|`, to turn everything into a Slip as it was zipped, but `Z` interprets `|` as the infix op that creates a Junction, instead of the prefix op that Slips/flattens. 13:11
That third form, `flat [Z] list_of_arrays`, clusters the commands to flatten and zip away from the lists-to-be-zipped, so I favor it in my own code, but I recognize that others might reserve `[Z]` for 3+ lists and always use infix-`Z` when there are only 2 lists.
Util Hmmm. That was meant to notify Nemokosch#9980 . I am working out how to best juggle IRC, Discord, and Bot-interactions. 13:13
Nemokosch why did [Z] work though?
that be me so you did right, lol
13:15 jgaz joined
Util Do you know about the [op] reduction meta-operator? 13:15
docs.raku.org/language/operators#R...aoperators
Nemokosch yes 13:16
Util `[Z] @m, @n, @o` is the same as `@m Z @n Z @o`, so I don't know why it *wouldn't* work. Can you clarify your point of confusion? Perhaps I have mis-understood your original question. 13:19
Nemokosch okay nevermind I just missed flat in front of it lol 13:30
so yeah here it's the same as infix:<Z> <a b c>, <m n o> 13:31
13:43 frost joined 14:06 frost left
Anton Antonov How can declare a proto for a function that takes 0 or 1 arguments? 15:57
guifa Anton: huh. You might not be able to actually. 16:02
You'll probably just have to use the general proto sub foo (|) { … }  16:03
Anton Antonov Yeah, that is what I am doing now. (Using |.) 16:09
stevied is there a shorter way to write this: `say $block-meta<explanation> if $block-meta<explanation>;` 16:32
16:36 dakkar left
Nemokosch m: .say if $_ given $block-meta<explanation>; 16:38
well okay...
m: .say if $_ given 0 16:39
gee gee 16:40
m: .say if $_ given True
<@563790557831495691> do you not want definedness instead by any chance? 🥺
stevied yeah, I think i should probably switch to definedness. I'm using an empty string if there's nothing to print but probably shouldn't do that 16:41
Nemokosch with definedness, suddenly there are choices 16:42
`with`, `andthen`, the `//` operator...
stevied right, i can use with/when
thanks. 16:44
Nemokosch although I'd say the `.say if $_ given $something` is still a win for logic value
`if $_` is a bit noisy but one doesn't have to repeat the actual term
stevied interesting
gfldex m: my $block-meta = explanation => 42; $block-meta<explanation> andthen .say;
stevied jesus, the are a million tiny details with raku. you really have to be coding in it for a while to get proficient
Nemokosch this is what I feel, too 😅 16:46
stevied there's lots of nifty little efficiencies but you really have to dig deep to take advantage of them
Nemokosch but I'm using these "scripty" shortcuts regularly, while I never started with OO for example
gfldex <@563790557831495691> not as hard as this: cdn.abcotvs.com/dip/images/5294602...jpg?w=1600
stevied heh
gfldex I wouldn't mind food pedals for typing unicode operators tho. 16:48
Nemokosch and you know, if there are features, you are tempted to use them
stevied gfycat.com/celebratedforsakencowrie
Nemokosch ```perl 16:49
my %repo-path <== <esko kako> Z ($esko-repo-path, $kako-repo-path)>>.IO andthen .flat;
```
and I did nail the precedence, lol
I hope you do, too 😂
stevied It appears I have a lot to learn from that examples. jesus 16:54
I don't recognize most of those operators
alright, so I guess the lesson is to avoid the empty string `''` with raku. 16:55
Nemokosch in what regard?
stevied to convey that a string should be treated as kind of a NULL value 16:56
Nemokosch yeah well it is a fully valid value afaik
stevied right, I mean, you can still use it, but it's better to use definedness 16:57
Nemokosch not even the default value of a string variable
Frankly I don't even like howthe empty string converts to false in most languages I've come across 16:58
stevied so instead of passing a `''` to a function, pass a `Str`
Nemokosch having it convert into 0 is even worse
for example, if you have an Str $variable 17:01
it's (Str) by default
$variable = Nil resets it to this default
idk if there is some dedicated way to reset it to default
my '' rant is unrelated to this haha
stevied well, anyway, I now have `.say with $block<explanation>;` now and all is right with the world 17:05
Nemokosch that's pretty nice imo
guifa the if / with distinction is one that should probably be emphasized a bit more for early Raku tutorials 17:18
Nemokosch I have yet another seq manipulation task that may not be a one-liner or at least I don't know how 17:25
I want to be able "batch" based on some predicate, rather than a fixed amount of items 17:26
like perhaps the number of digits 😄 17:27
so the output should be like ((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (10, 11, 12 ...... 99), (100, 101, .....
17:54 Nemokosch joined 17:57 thundergnat joined
thundergnat Nemokosch: categorize perhaps? 17:57
m: print .value[^5].gist for sort (^50000).categorize: *.chars 17:58
camelia (0 1 2 3 4)(10 11 12 13 14)(100 101 102 103 104)(1000 1001 1002 1003 1004)(10000 10001 10002 10003 10004)
Nemokosch Doesn't work on infinite sequences
thundergnat Ah. No, it wouldn't...
Nemokosch right now I know nothing better than a stateful map 18:01
18:09 Nemokosch left
stevied i got another little problem Im stuck on: 18:11
```
for $section<code>.Array {
$_>>.trim;
das-new $_[0], $_[1], $_[2];
}
```
thundergnat Maybe List::Divvy would be usable? (Shameless plug) 18:12
won't work in Camelia but: use List::Divvy; my $list = ^∞; print $list.&bounded(10**($_-1), 10**$_)[^5].gist for 1..6;
yields: (1 2 3 4 5)(10 11 12 13 14)(100 101 102 103 104)(1000 1001 1002 1003 1004)(10000 10001 10002 10003 10004)(100000 100001 100002 100003 100004) locally
stevied how do I send that last line without having to send separate args? 18:13
I tried .flat but that sends a sequence
thundergnat raku.land/zef:thundergnat/List::Divvy
das-new .[0,1,2]; maybe? 18:14
Nemokosch is it 3 long? 18:15
stevied for now, but would be nice to be able to grow it to accept additional elements if necessary
Nemokosch okay but you want them all, right?
seems to me |$_ would do the trick 18:17
stevied tried that, gives me an error: `Type check failed in binding to parameter '$code'; expected Str but got Slip (slip("1, 2;", "This ...)` 18:18
oh, maybe because the .trim does something I'm not expecting 18:19
nope, taking out .trim doesn't work
Nemokosch well, what is this das-new?
because `say |$_` worked for me as expected
stevied `sub das-new(Str:D $code, Str $description = Str, Str $explanation = Str ) `
thundergnat If das-new wants a string you have to give it a string 18:20
try das-new .gist;
Nemokosch okay but it could take it out from that slip, lol 18:21
stevied it is a string
Nemokosch "1, 2;"
stevied I don't get it. `das-new $_[0], $_[1], $_[2];` works 18:22
Nemokosch I noticed that .Slip and | don't behave the same
but | seemed to work for function calls, unlike .Slip
thundergnat or maybe das-new .flat;
stevied tried it 18:23
Nemokosch that won't unpack for sure
stevied ok, got it working by removing the parens: `das-new !$_`
huh
Nemokosch not with the exclamation mark I hope...
😄
stevied oh, whoops
Nemokosch that would be harsh 18:24
stevied ok, got it working by removing the parens: `das-new |$_`
ugh, ok, it was the space between the function name and the parens
Nemokosch that's important indeed
blah(x) and blah (x) aren't the same call.. 18:25
stevied works with parens and no space: das-new(|$_);
right
Nemokosch I mean maybe with one argument they are but yes
the first type of paren is for the function call, the second is just for the data
these features are like double-edged swords tbh
you need to be really wary of whitespace 18:26
like what I noticed half an hour ago
if 0 { say 'End of the world'; } say 'Just kidding'; 18:27
will fail
stevied yeah, one of those things that I'm focused on solving one problem I didn't think of the obvious problem
Nemokosch because that damn block actually wants a semicolon, like in shell...
so a non-terminal block will almost certainly be ugly
a related thing: blocks will imply the semicolon at the end of the line, even if they are inside a method call... 18:29
@stuff.map: -> $a, $b { $a + $b }
.say
will not be what you wanted
if you add `andthen` inbetween, you will get an error that the statement starts with `andthen` out of the blue 18:30
so I think if somebody doesn't want to spend too much time on these nuisances, it's better to use some mainstream convention 18:32
function calls with parens immediately after the function name
method calls similarly
blocks on end of lines
jgaz I have two arrays, a really big one and a smaller one of 5 or 10 elements. How can I easily check to see if the small array exists inside the big array? 19:40
Nemokosch what counts as "exist inside"? 20:28
the elements are present? one is a slice of the other? 20:29
for the former: @small (<=) @big 20:32
SmokeMachine jgaz: something like this? glot.io/snippets/gapahyqhto 20:33
Nemokosch subset operator 20:34
for the latter, I would pick
`@big ~~ (**, |@small, **)`
put it into backticks because markdown is triggering me xd
hm... is there something wrong with the smartmatch here? 20:36
it seems so much simpler it's overwhelming 😅
jgaz Thanks, Nemokosch, I'll give that a try.
SmokeMachine Great solution Nemokosch!
Util Easy but inefficient: 20:37
jgaz I assume `()` gives everything a List context. What do the two pairs of asterisks and the pipe do?
Util @big.join("\0").contains( @small.join("\0") );
Easier but still inefficient, and also buggy if elements can contain spaces:
@big.Str.contains( ~@small );
Better:
my List $L = @small.List;
my Pair $size = @small.elems => (1 - @small.elems);
@big.rotor($size).first(* eqv $L).defined;
# May be much more efficient, depending on how many places the first element of @small occurs in @big.
@big.grep(:k, { $_ eq @small[0] }).first({ @big.skip($_).head(+@small).cache eqv $L }).defined
Nemokosch I would be curious how the smartmatch is implemented 20:44
jgaz: the parens actually account for the right precedence and pretty much that's about it
the pipe unpacks the elements 20:45
jgaz Ah 20:46
Nemokosch and ** is the so-called HyperWhatever here
docs.raku.org/type/HyperWhatever
basically it says "some random elements" in smartmatching context
whereas the single asterisk Whatever would mean exactly one element 20:48
gfldex <@297037173541175296> my shot at a fancy `batch`: gist.github.com/e28992d7d85451ed66...42f0b27e30 20:57
jgaz Nemokosch: I can confirm that works and it's reasonably performant against an array 8kB in length. 20:59
Nemokosch 🥳 21:00
jgaz Thanks a lot for the help. It was driving me a little crazy. 21:01
21:19 jgaz left
stevied anyone know how I can list files in the `resources` directory in a module? is it possible? 21:28
guifa No, it's not 21:39
The idea is that all resources should be known at compile time
Or rather, not at compile time, but at development time
stevied ok. so what's a good way to store text files? Each text file in the module will contain a "lesson". The number of lesson will grow over time and I want to gradually add them to the module. 21:42
should I just dump them into a directory in lib?
and then I would use the $?DISTRIBUTION hash to find and load them? 21:44
ok, yeah, looks like that works 21:46
guifa That's one way. I just have a script for Intl::CLDR that processes each one, creates resources/languages.txt that's a newline delimited file that I can use to know which languages are available 22:01
actually, I take that back. I don't think that $?DISTRIBUTION is supposed to provide introspectable IO::Path directories 22:03
22:42 Nemokosch joined 22:55 Nemokosch left
stevied when you say "not supposed to provide," what do you mean exactly? 22:55
it seems to work fine. 22:56
23:05 thundergnat left
Util <@195453211409121280> I am late to the conversation, but I am confused by the use of Iterator in that Gist. Am I missing some corner-case? Any of these three look simpler, to my eye: gist.github.com/Util/efc065591a311...cc474c9959 23:38