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.
04:38 teatime joined
pelevesque What is the most elegant way to get all the permutations of a list, but limit it to a given number. <a b c d 05:20
ab5tract_ so you want only N elements of all the permutations of a list? 07:35
m: say <a b c d>.permutations.head(5) 07:37
camelia ((a b c d) (a b d c) (a c b d) (a c d b) (a d b c))
ab5tract_ plevesque: if you only wanted to compute a given number of the permutations, instead of all of them and then truncate, you could use `gather` / `take` and a hand-rolled permutation algorithm to generate them lazily. However, it might very well be slower to do it that way 07:44
pelevesque my @symbols = qw[! " # $ % & ' * + , - . / : ; < = > ? @ \ ^ _ ` | ~ ]; my $i = 0; for (@symbols X~ @symbols) -> $combo { say "{++$i} $combo" } I ended up with this and it works.
The X~ is what I was looking for.
I ended up perusing the docs for awhile and learning a zillion other things in the process. What a beautiful language. 07:45
ab5tract_ pelevesque: ah, `X` grand. I was going to suggest that first but its functionality is different than classic list permutuation. glad it works for you! 07:48
* `X` is grand
pelevesque X is grand... and Raku is grandiose! 07:52
ab5tract_ pelevesque: One small suggestion for your example. Have you heard of state variables yet? 07:57
pelevesque No, I haven't. I just rewrote my code like this, and much prefer it. perl my @symbols = qw[! " # $ % & ' * + , - . / : ; < = > ? @ \ ^ _ ` | ~ ]; my $i = 0; say "{++$i} $_" for @symbols X~ @symbols; 08:03
ab5tract_ ah, yes `for` as a statement modifier is quite nice! 08:08
so about state variables: they keep their state between iterations of a loop / executions of a block 08:09
m: for ^3 { state $x = 0; $x.say }
camelia 0
0
0
ab5tract_ m: for ^3 { state $x = 0; ++$x.say } 08:10
camelia 0
Cannot resolve caller prefix:<++>(Bool:D); the following candidates
match the type but require mutable arguments:
(Mu:D $a is rw)
(Int:D $a is rw --> Int:D)
(int $a is rw --> int)
(uint $a is rw --> uint)
(Bool $a i…
ab5tract_ m: for ^3 { state $x = 0; (++$x).say }
camelia 1
2
3
ab5tract_ there we go.
pelevesque Ah, that is sweet! love that.
ab5tract_ now, that could have been used to move your declaration of `$x` into the for loop block 08:11
pelevesque I love how it makes it possible to bring the counter inside the loop where it kind of belongs iinstead of out in the global space,
ab5tract_ but now you are just passing a single expression instead of a block to for
wait: it gets even cooler :D
pelevesque right
ab5tract_ so, since that idiom is so common (`state $idx = 0; @foo[$idx++]`), Raku includes a short-hand for this using _anonymous_ state variables 08:12
pelevesque show me! 08:13
ab5tract_ the above can simply be written as `@foo[$++]`
pelevesque That's nuts!
love it
ab5tract_ note you can only use a given anonymous state var in a single location. `{ @foo[$++]; say $ }`, for example, will print `(Any)` because you've created two anonymous state variables here, and the second one has never been defined to anything 08:16
pelevesque Ah, just one thing i would improe in the language!
ab5tract_ enjoy :D
sure, what's that?
pelevesque $++ counts 0 to more... but ++$ does as well. I was hoping ++$ would count from 1 to more. 08:17
ab5tract_ seems to work for me 08:18
m: my @f = <a b c>; for ^3 { say @f[$++]; say (++$) }
camelia a
1
b
2
c
3
ab5tract_ m: my @f = <a b c>; for ^3 { say @f[$++]; say ++$ }
camelia a
1
b
2
c
3
pelevesque m: say "test"; 08:19
Raku eval test
pelevesque m: my @f = <a b c>; for @f { say $++ } 08:21
Raku eval 0 1 2
pelevesque m: my @f = <a b c>; for @f { say ++$ }
Raku eval 1 2 3
pelevesque oh my god... it works.. I must have tested wrong... sorry
ab5tract_ no worries :)
that would be a big breakage! 08:22
pelevesque m: say $++ ~ " $_" for <a.b c> 08:28
Raku eval 0 a.b 1 c
pelevesque m: say ++$ ~ " $_" for <a b c>
Raku eval 1 a 2 b 3 c 08:29
pelevesque I was hoping this would work: m: say "{++$} $_" for <a b c> 08:30
lakmatiol Ye, that one won't, since "{}" creates a new block. 08:33
docs.raku.org/language/traps#Using...state_vars 08:34
pelevesque Thanks everyone. 08:47
With the help of this amazing community, this amazing language, and the amazing docs. In a matter of a few hours I went from this: #!/usr/bin/env raku sub MAIN() { # Notice the sad escapes \< and \> in the line below. my @symbols = <! " # $ % & ' * + , - . / : ; \< = \> ? @ \ ^ _ ` | ~ >; my $i = 0; for (@symbols X~ @symbols) -> $symbol-combo { say "{++$i} $symbol-combo"; 08:52
} } To this: #!/usr/bin/env raku unit sub MAIN; my @symbols = qw[! " # $ % & ' * + , - . / : ; < = > ? @ \ ^ _ ` | ~ ]; say "$(++$) $_" for @symbols X~ @symbols;
It is a really amazing feeling... and Raku and Community really brings the job of coding back. 08:53
ab5tract_ pelevesque: our target has for a long time been -Ofun
:)
pelevesque You hit the target on the spot. This will sound ridiculous, but programming in Raku is the fun for adults that is similar to the fun I had doing BASIC on a ZX81 back when I was 6 years old. It's like the fun factor is the same, only the language is hyper powerful. 08:55
lizmat :-)
ab5tract_ pelevesque: We've tried our best for always doing the sane thing under such hyper powerful demands/expectations but of course there can be some curious corners on the way through learning Raku. So don't hesitate to share any surprises that you encounter :) 09:04
Have you already been introduced to `*`? 09:05
pelevesque I know the whatever operator, but not *? 09:06
ab5tract_ m: dd (^10).grep(*.is-prime) 09:09
camelia (2, 3, 5, 7).Seq
ab5tract_ m: dd (^10).grep(* %% 3)
camelia (0, 3, 6, 9).Seq
ab5tract_ m: (* %% 3).WHAT.say 09:10
camelia (WhateverCode)
ab5tract_ m: dd (* %% 3) # not sure why I did it the hard way last time :) 09:12
camelia WhateverCode.new
ab5tract_ it's often called currying but apparently that's not 100% CS terminology accurate 09:13
pelevesque That is really cool. 09:14
ab5tract_ saves a lot of typing :) 09:17
pelevesque Raku seems good at not only saving typing, but making code even clearer in the process.
ab5tract_ plus once I got used to it, I found `* %% 3` easier to quickly parse than `{ $_ %% 3 }` 09:18
Re: Clarity -- TimToady (Larry Wall) was always fascinated by Huffman coding, as were many language designers that he inspired. It's not the ideal appreciated by everyone I guess, but I sure like it. 09:24
sdomi mmm... what's the "correct" way for handling whitespace in regexps in raku? suppose I want to `s:g/.*? /asdf /`; this doesn't work correctly, unless I replace the space with \s, or quote the whitespace part 09:43
the 2nd one is ugly (`s:g/.*?' '/asdf' '/` - i'm surprised this even works) 09:44
but I seemingly can't quote the whole thing, because then it will become a constant
lizmat by default, whitespace in regexes is not significant
sdomi hmm
lizmat if you're coming from Perl: /x by default
sdomi i'm coming from (abusing) bash, so i may have a different outlook
lizmat ah, ok
sdomi :p
decided to bite the bullet because while I know that I could do anything in bash, this seems to be comfier 09:45
lakmatiol You can do .*? ' ' /asdf ' ' which should be a bit less ugly.
sdomi tbh just knowing why this happens (and that I can insert as much space as I want in the regexp, right?), this is easier to swallow :p 09:46
lizmat so you want to match any number of characters followed by a space by 'asdf ' ?
*replace 09:47
sdomi yup 09:52
that's just a test case, i'm trying to get accustomed with raku 09:53
lizmat is the boundary a single space, or any sequence of whitespace characters?
sdomi i wanted it to be a single space :p
lizmat s:g/.*? )> " " /asdf/ 09:57
the )> is the capture marker 09:58
so anything before that will be the "capture" and *that* will be substituted
m: $_ = "foo bar "; s:g/.*? )> " " /asdf/; .say
camelia asdf asdf
ab5tract_ sdomi: you can indeed have as much whitespace, including newlines, in your regex as you would like 09:59
lizmat m: $_ = "foo bar "; s:g/<-[\ ]>+ )> /asdf/; .say # much better performant sdomi 10:06
camelia asdf asdf
lizmat basically look for 1 or more characters that are *not* a space: <-[\ ]>+ 10:07
sdomi lizmat: woo, fancy
I've never used triangle brackets in regexp before
is that a raku thing, or did I somehow miss a pcre feature?
lizmat well it's not really a triangle bracket 10:08
it's a backslash to escape the space
sdomi that I see
lizmat <[]> is an enumerated character class: docs.raku.org/syntax/%3C%5B%20%5D%3E
sdomi ohhh this is cool! 10:09
lizmat the - in <-[ negates 10:10
ab5tract_ sdomi: Raku regexes are a condensation of all the learnings from Perl's infamous regex capabilities
sdomi lizmat: yeah yeah, i get it now ^^
ab5tract_ so yeah, it's all very Raku-specific in the sense that nothing like a theoretical `rcre` has popularized Raku's regex syntax 10:11
sdomi i've abused perl regexes (in GNU grep -P, mostly) a ton, so I feel I'm gonna get *so much mileage* out of those extra features in raku
ab5tract_ if you know your way around regexes, I expect you will find a lot to love -- docs.raku.org/language/regexes :D 10:12
nemokosch to be honest, I think the fact that you can just quote everything that you want to be literally in your regex match, rather than having to memorize what you should escape and what you shouldn't, is enough of a feature 10:13
pelevesque I read the book on regex by Moritz and was jaw dropped on each page. 10:14
lakmatiol Yeah, regexes are definitely a highlight of raku 10:29
11:39 teatwo joined 11:42 teatime left
codesections sdomi: Also, there's also the :sigspace (or :s for short) modifier if you *want* regexes to have significant whitespace 13:32
docs.raku.org/language/regexes#Sigspace
14:40 MasterDuke left 15:19 tonyo joined 16:46 andinus joined, andinus left, andinus joined 21:07 ab5tract joined 21:40 ab5tract left 22:53 teatime joined 22:55 tea3po joined 22:57 teatwo left 22:58 teatime left 23:33 teatwo joined 23:37 tea3po left 23:40 teatime joined 23:41 teatime left 23:43 teatwo left, teatime joined 23:44 teatime left, teatime joined, teatime left