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.
00:03 Kaipei left, Kaipei joined 00:06 kjp left
stevied how can I turn this into a one-liner? 00:41
```
method topics() {
my @topics;
@.directories.map: { @topics.push(.name) if .depth == 1 };
return @topics;
}
```
ok, got it `map( { .name if .depth == 1 }, @.directories)` 01:10
01:11 frost joined
or `@.directories.map: { .name if .depth == 1 };` 01:11
guifa why not use grep? 01:15
stevied I couldn't figure it out
guifa @.directories.grep(*.depth == 1).map: *.name
also, any reason you're using method access rather than direct attribute access? (not necessary wrong, but it's not common, so I'm curious) 01:16
stevied i'm calling the method from a test file 01:19
seems a little more elegant without grep, actually 01:30
when I was trying grep, I was trying to use with the hyper operator. that was my problem 01:31
04:32 kjp joined 07:02 dakkar joined 10:01 frost left 10:22 lizmat_ joined 10:26 lizmat left, lizmat_ is now known as lizmat 10:44 Nemokosch joined 10:56 frost joined 11:10 discord-raku-bot left, discord-raku-bot joined
I've got this method calls: `method resource-exists(Str:D $path)` 11:17
I want to check that the $path starts with a certain directory. if it doesn't, I want to modify it before it gets used by the method. is this possible? 11:19
SmokeMachine `Str:D $path is copy`? 11:20
stevied it's not set up as `is copy` no 11:23
what can that get me?
lizmat that you can modify it 11:25
m: sub a($a is copy) { $a = 42; say $a }; a 666
camelia 42
stevied right, but what does it get me in relation to my problem of wanting to modify the argument before it's used by the method? 11:27
lizmat m: sub a($a is copy) { $a = 42 if $a == 666 }; a 666; a 314 11:28
camelia ( no output )
stevied might this be what a callable is for? I have never used a callable argument before.
SmokeMachine staved: it let you modify the argument received by the method. I've probably misunderstood something...
lizmat m: sub a($a is copy) { $a = 42 if $a == 666; say $a }; a 666; a 314
camelia 42
314
stevied yeah, but is there a way to modify the argument without putting that logic inside of the method? 11:29
like, I want to run a little subroutine to do the argument checking, is what I guess I mean 11:30
lizmat well, you could make it a multi
m: multi sub a(666) { a 42 }; multi sub a($a) { say $a }; a 666; a 314 11:31
camelia 42
314
stevied I want to test if `$path` starts with `blah/` and if it doesn't, then tack on `blah/` to the front
lizmat m: multi sub a($a where *.starts-with("blah/") ) { say $a }; multi sub a($a) { a "blah/$a" }; a "blah/foo"; a "bar" 11:34
camelia blah/foo
blah/bar
SmokeMachine m: multi bla($path where *.starts-with: "bla/") { bla "somewhere/else/" }; multi bla($path) { say $path }; bla "bla/qwer"; bla "ble/qwer" 11:35
camelia somewhere/else/
ble/qwer
11:36
SmokeMachine m: multi bla($path where *.starts-with: "bla/") { callwith "somewhere/else/" }; multi bla($path) { say $path }; bla "bla/qwer"; bla "ble/qwer"
camelia somewhere/else/
ble/qwer
stevied I guess that works. might get a little mess with a bunch of multis for each of my methods that use `$path` 11:37
i guess I'll just remember to put a little path-check() method in the body of each method. 11:39
thanks
i'll use multi for the `path-check` sub 11:42
SmokeMachine stevied: maybe you could do something like this: 11:48
m: multi trait_mod:<is>(Routine $ref, Bool :$path-checked) { $ref.wrap: -> $path, |c { do if $path.starts-with: "bla/" { callwith "other/path", |c } else { callsame } } }; sub do-something-with-path($path) is path-checked { say $path }; do-something-with-path "bla/ble"; do-something-with-path "ble/bli"
camelia other/path
ble/bli
stevied that's hard to read in discord. it's all on one line. can you put that in a paste bin or something, please? 11:50
SmokeMachine m: gist.github.com/FCO/088dd067dcce83...5c5328d56e 11:53
camelia ===SORRY!=== Error while compiling <tmp>
Two terms in a row across lines (missing semicolon or comma?)
at <tmp>:21
------> do-something-with-path "ble/bli"⏏<EOL>
expecting any of:
infix
infix stopper
Nemokosch dw it's hard to read in IRC as well
SmokeMachine m: gist.github.com/FCO/088dd067dcce83...5c5328d56e 11:54
camelia other/path
ble/bli
something else: other/path
something else: ble/bli
stevied taht's super cool 11:55
SmokeMachine (sorry, missing `;`)
11:58 discord-raku-bot left, Nemokosch left, discord-raku-bot joined
stevied ok, I knew nothing about traits: docs.raku.org/type/Sub#index-entry...eclarator) 11:58
interesting
SmokeMachine stevied: I think that would work even better it $path was named instead of positional... 12:02
stevied i'm running into one small problem. the trait_mod method can't seem to get access to the `self.root` attribute in the class it's being used in
12:05 discord-raku-bot left, discord-raku-bot joined 12:06 jgaz joined
ok, running into issues. the `trait-mod` sub is inside a class. so the first arg is the invocant 12:17
here's the error I get: 12:23
```
No such method 'starts-with' for invocant of type
'Doc::Resources::Resources'
in block at /Users/stevedondley/git_repos/raku/modules/Doc-Examples/lib/Doc/Resources.rakumod (Doc::Resources) line 44
in block <unit> at /Users/stevedondley/git_repos/raku/modules/Doc-Examples/t/03-resources.rakutest line 12
```
13:15 jgaz left 13:25 lizmat_ joined 13:27 lizmat left 13:32 lizmat_ is now known as lizmat 14:07 frost left
ok, managed to figure out how to do a trait_mod to wrap a method: 14:19
```
multi sub trait_mod:<is>(Method $obj, Bool :$path-checked) {
$obj.wrap: -> $inv, $path, |c {
if $path.starts-with: 'resources/examples/' {
callsame;
} else {
callwith $inv, "resources/examples/" ~ $path, |c;
}
}
}
```
now, the last tricky bit is that I want to look up the path which is a `$.root` attribute 14:20
I can't get access to it from inside the trait_mod sub 14:21
I tried doing `multi method trait_mod` but just got an error:
`Can't use unknown trait 'is' -> 'path-checked' in method declaration.`
is there any way to get around that?
SmokeMachine why not just call $inv.root? 14:23
stevied ah, lemme try
bam, nice. 14:24
thanks! 14:25
i'm at peace again 🙂
this is exactly the kind of thing I was looking to do. very cool
this is some pretty amazing stuff, at least to me 14:27
14:36 Kaipii joined 14:39 Kaipei left 14:41 Kaipii left, Kaipii joined 15:23 sienet_ja_LSD[m] joined 15:38 dakkar left 16:14 Kaipii left, Kaipii joined 16:36 jgaz joined 16:57 jgaz left
guifa traits can be crazy useful in some instances 17:13
I have one that lets you use alias method names. Took about 5-10 minutes to write, but man, it saves a LOT of time (and improved user experience for folks using the module :-) ) 17:14
Nemokosch which module? 🧐 17:25
guifa Intl::CLDR
github.com/alabamenhu/Intl-CLDR/bl...akumod#L38 17:26
LOL my declarator pod is backwards between the attribute and method trait. note to self to fix it 17:27
I did it because CLDR has been around for a while, and depending on the era, some parts use one or another naming convention. I default to lowercase kebab because that's the norm in Raku, but if that's not what it uses, I provide the alias in case that makes life easier integrating with other workflows 17:40
17:48 johnjaye joined
johnjaye is most work that happens on raku on the language or libraries or something else? 17:48
Nemokosch what is CLDR 😅 17:50
johnjaye some unicode xml-ish thing for locales. sounds terrifying 17:53
Nemokosch lol xd 17:59
johnjaye: rakudoweekly.blog/ check this out 18:06
sienet_ja_LSD[m] what data format is h' ? 18:33
like in python they have b'a0a786a8' for bytes data 18:34
that starts with h'
21:24 Kaipii left
guifa I would assume it's for hex, but it's not a Raku-based format 21:26
21:27 jgaz joined
guifa johnjaye both. There are still parts of the language that are being worked on (macros, etc) but since everything is there for 99% of projects, lots of us are now focusing on developing libraries to use with it 21:27
sienet_ja_LSD[m] aye ok, CBOR::Simple produced it (which I still don't seem to get working)
guifa Nemo: yeah, it's a massive database of localization data, like, how do you format dates, what are the abbreviations for months, etc. 21:28
21:36 jgaz left
Anton Antonov I have a nested hash (say, a hash of hashes of hashes) `%h3` and a list of keys `<k1 k2 k3>`. What is the shortest code to get to the "bottom" element with that list of keys? I know I can do `%h<k1><k2><k3>` and, maybe, I can write a reduce-like code. Is there anything shorter and/or more standard? 21:50
22:06 m_athias_ joined 22:07 pi1 joined, m_athias left, deadmarshal left, johnjaye left 22:10 m_athias_ is now known as m_athias 22:13 deadmarshal joined 22:25 pi1 is now known as johnjaye
johnjaye how easy is it to port a perl library to a raku library on a scale of 1 to 10? 22:26
22:33 jgaz joined
guifa Anton: nothing standard. I'd just write a quick sub that does it, and call it 22:38
m: sub deep(%b,+@k) {my $r=%b; $r=$r{$_} for @k;$r}; my %a = b=>%(c=>1,d=>2),e=>%(f=>3,g=>4); say %a.&deep: <b c>; say %a.&deep(<e g>);
camelia 1
4
guifa if you're really going to use it a lot, you might look at creating a custom postcircumfix, but since the standard (post)circumfixes have all been taken, it'll be tricky coming up with a good choice, but you could do 22:40
multi sub postcircumfix:<⟨ ⟩>(%b,+@k) {my $r=%b; $r=$r{$_} for @k;$r}; my %a = b=>%(c=>1,d=>2),e=>%(f=>3,g=>4); say %a⟨<b c>⟩; say %a⟨<e g>⟩; 22:41
the catch is that the %a<foo> is actually shorthand for %a{'foo'}, so it's combining a quote and an operator in one. You can only create the custom operator side, so you'd still need to create an array of strings (via < >) 22:44
Anton Antonov @guifa Thanks! The postcircumfix solution is nice. (But I used double squared brackets, "⟦ ⟧", instead of angular brackets.) 22:51
BTW, I mostly want / need this for testing of Tries with frequencies; see raku.land/cpan:ANTONOV/ML::TriesWi...equencies. So, having that kind function defined in that package would make sense. 22:54
guifa BTW, I just tested and you can use ASCII double square brackets without any trouble :-) 22:55
multi sub postcircumfix:<[[ ]]>(%b,+@k) {my $r=%b; $r=$r{$_} for @k;$r}; my %a = b=>%(c=>1,d=>2),e=>%(f=>3,g=>4); say %a[[<b c>]]; say %a[[<e g>]]; 22:56
m: multi sub postcircumfix:<[[ ]]>(%b,+@k) {my $r=%b; $r=$r{$_} for @k;$r}; my %a = b=>%(c=>1,d=>2),e=>%(f=>3,g=>4); say %a[[<b c>]]; say %a[[<e g>]]; say [[1,2], 3]
camelia 1
4
[[1 2] 3]
guifa Might make typing easier. Shouldn't be a problem, as you can create raw lists like @a = [[1, 2], [3,4]] without interference (those are circumfixes, rather than postcircumfixes), and the likelihood of you doing needing to pass a literal array as an index of an array (like `@foo[[1,2] + 3]` or something) is probably about zero :-) 22:58
Anton Antonov Thank you, sounds good! 23:10
stevied I can't figure out how to use splitpath: docs.raku.org/routine/splitpath 23:32
just get an error: `No such method 'splitpath' for invocant of type 'Str'` 23:33
I tried converting it to .IO but that didn't help
this is my code:
```
method has-separator(Str:D $path) {
($path.splitpath)[1] ?? True !! False;
}
```
23:42 jgaz left