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. |
|||
shad.ow | to flatten out the 2d list before summing it | 00:15 | |
nemokosch | why not just use .flat? | ||
shad.ow | that works ? | 00:17 | |
perl [0] > [[1, 2, 3], [4, 5, 6]].flat ([1 2 3] [4 5 6]) | |||
am i misunderstanding something | |||
nemokosch | I think your original case would work | ||
shad.ow | wdym | 00:18 | |
perl my @lines = "input.txt".IO.lines; my %gears; for @lines.kv -> $j, $line { for $line.comb(/<-[\d.]>/, :match) { %gears{.from + $j * i} = []; } } for @lines.kv -> $j, $line { for $line.comb(/\d+/, :match) { for ($_.from - 1)..($_.to) -> $y { for ($j - 1)..($j + 1) -> $x { %gears{$y + $x * i}.append(+$_) if %gears{$y + $x * i}:exists; } } } } say (gather | |||
%gears.values.deepmap(*.take)).sum; say %gears.values.map({ .elems == 2 ?? $_[0] * $_[1] !! 0 }).sum; | |||
nemokosch | but to be fair I'd have to check | ||
shad.ow | this is what i have rn | ||
it's a 2d list | |||
the values in the hash are lists | |||
the keys are complex numbers | |||
representing 2d coordinates | |||
nemokosch | oh damn, not this again... | 00:19 | |
why even have flat if it "respects" containers, and why do containers "itemize"... | |||
shad.ow | >>.List.flat seems to work | 00:20 | |
nemokosch | m: my %gears = a => (1, 2), b => (4, 6); %gears.values.map(*.Slip).say; | ||
Raku eval | (4 6 1 2) | ||
nemokosch | this also works | ||
shad.ow | how does that work | ||
perl %gears{$y + $x * i}.append(+$_) if %gears{$y + $x * i}:exists; | |||
also is there a better way to write this | |||
nemokosch | a Slip is a "List without the parens around it" | 00:21 | |
shad.ow | append to a list in a hash if it's in the hash | ||
ahh | |||
nemokosch | meaning that it auto-flattens into its context | ||
fortunately, the method call eliminates the Scalar containers | |||
m: ($(1, 2).Slip, (3, 4).Slip).say | 00:22 | ||
Raku eval | (1 2 3 4) | ||
nemokosch | does the same for both | ||
shad.ow | i see | ||
sorry yesterday was my first day with raku / perl | |||
kind of pieceing things together as i go | |||
i am doing advent of code in a different language each day lmao | 00:23 | ||
cool language so far tho | |||
any ideas for this pattern ? | |||
nemokosch | this is always an active time of the year... kind of | ||
shad.ow | yeah real | 00:24 | |
00:24
teatime joined
|
|||
nemokosch | well, you already have a topic so it kinda pushes one from using a shortcut that makes use of the topic variable | 00:25 | |
shad.ow | because if i remove that if | ||
it'll initialize an empty list in the gears | |||
nemokosch | with %gears{$y + $x * i} -> $current-gear { $current-gear.append(+$_); } | ||
not exactly the same I think, not sure how the :exists check works | |||
this checks for an undefined value | |||
shad.ow | ahh interesting | ||
i guess what i have now is fine | 00:27 | ||
perl my @lines = "input.txt".IO.lines; my %gears; for @lines.kv -> $j, $line { for $line.comb(/<-[\d.]>/, :match) { %gears{.from + $j * i} = []; } } for @lines.kv -> $j, $line { for $line.comb(/\d+/, :match) { for ($_.from - 1)..($_.to) -> $y { for ($j - 1)..($j + 1) -> $x { %gears{$y + $x * i}.append(+$_) if %gears{$y + $x * i}:exists; } } } } say | |||
%gears.values.map(*.Slip).sum; say %gears.values.map({ .elems == 2 ?? $_[0] * $_[1] !! 0 }).sum; | |||
this is quite gorgeous | |||
thanks for the help ! | |||
perl say ($_[0] * $_[1] if .elems == 2 for %gears.values).sum; | 00:33 | ||
this is nice too | |||
ohhh i see waht you mean now | 00:38 | ||
00:54
sdomi left
00:55
sdomi joined
02:56
swaggboi left
03:02
swaggboi joined
|
|||
jaumegreen | I have a doubt with SetHash, how do I set and check a key via variables? Let me explain: my %numbers is SetHash; my $factor = 10; my $posy = 2; my $posx = 3;say "empty ",%numbers.keys.sort; #empty () say "empty ",%numbers<$posy*$factor+$posx>," ",%numbers<23>;#empty False False %numbers<$posy*$factor+$posx>++; say "vars ",%numbers.keys.sort; #vars | 07:45 | |
($posy*$factor+$posx) say "vars ",%numbers<$posy*$factor+$posx>," ",%numbers<23>;#vars True False %numbers.set($posy*$factor+$posx); say "set ",%numbers.keys.sort; #set ($posy*$factor+$posx 23) say "set ",%numbers<$posy*$factor+$posx>," ",%numbers<23>;#set True False %numbers{$posy*$factor+$posx}=True; say "key ",%numbers.keys.sort; #key ($posy*$factor+$posx 23) say | |||
"key ",%numbers<$posy*$factor+$posx>," ",%numbers<23>;#key True False So, it seems that 23 is set as a key (both the 2nd and 3rd way work), but at the same time it evaluates to false. And the only one that evaluates to true is the first one set, which stores the formula. I see no example with variables on the documentation, so I'm a little bit lost on why this doesn't work and how to make it work. Can anyone explain | |||
it to me? Thanks. | |||
rcmlz | m: my $key = 1; my %hash; %hash{$key} = $key; dd %hash; | 09:24 | |
Raku eval | Hash %hash = {"1" => 1} | ||
rcmlz | To my understanding accessing %hash<> requires a string between the <>. | 09:25 | |
whereas %hash{} also accepts variables. | 09:26 | ||
m: my $key = 1; my %hash; %hash<$key> = $key; dd %hash; | 09:27 | ||
Raku eval | Hash %hash = {"\$key" => 1} | ||
ab5tract | m: my $key = "k|"; my %hash; %hash«$key» = True; dd %hash.keys | 09:32 | |
camelia | ("k|",).Seq | ||
ab5tract | m: my $key = "k"; my %hash; %hash«$key» = True; dd %hash.keys | 09:33 | |
camelia | ("k",).Seq | ||
jaumegreen | Setting it it's not much of a problem, but the retrieval is problematic, neither %numbers<$key> nor %numbers<23>return true to me. | 09:35 | |
Nahita | %numbers<stuff> is doing %numbers{'stuff'} as a shortcut and single quotes don't interpolate, so 1) your formula is put literally there | ||
2) when you do %numbers<23>, you request %numbers{'23'}, i.e., string 23, not Integer, so it doesn't give you True | |||
because you set 23 the Integer with the .set method | 09:36 | ||
jaumegreen | ok, it seem that %numbers{$key} work. Thanks | ||
And thanks for the string vs integer explanation. It will probably help in the future 🙂 | 09:37 | ||
ab5tract | jaumegreen: or %h«$var», if you like those braces better | ||
well actually that won't solve the Int vs Str issue | 09:38 | ||
Nahita | FWIW, with <<stuff>>, when stuff is Integer, SetHash registers an IntStr | ||
unlike normal Hashes i guess | |||
so either .set method or {...}++ seems leading to more predictable behaviour | 09:39 | ||
ab5tract | that likely means that «$var» calls `val($var)`, which would make sense | 09:41 | |
nemokosch | That sounds problematic on many layers | 09:59 | |
DarthGandalf | Hi, how to make an array to be initialized with lazy sequence which always returns 0? I want to read non-existing elements from the array to sum them up, but that results in "Int + 42" | 10:48 | |
Nahita | hi, what about my @arr is default(0) or you can maybe .grep at the summing time | 10:51 | |
DarthGandalf | thanks, I couldn't figure how to use .grep for that - {$_==Int} doesn't work; but is default works | 10:54 | |
Nahita | i would grep defined ones instead of particularly Int ones (although for Int equality you can use triple ===), like .grep(*.defined) | 10:55 | |
because == is numeric equality checker | |||
DarthGandalf | ah, I was looking for .definite but that's only on how class >< | 10:56 | |
lizmat | DarthGandalf: if your int values fit within 64 bits, then you could also do: my int @array | ||
DarthGandalf | oh, even better | 10:57 | |
thanks | |||
11:48
foo63 joined
|
|||
ab5tract | m: (0, ‘a’, 11).grep(Int) ==> dd() | 12:41 | |
camelia | (0, 11).Seq | ||
ab5tract | The idiomatic way to check of something is of a type is to use smartmatch. The above thunks the type into the rhs of a smartmatch | 12:43 | |
DarthGandalf | m: (0, 'a', Int, 11).grep(Int) ==> dd() | 12:57 | |
camelia | (0, Int, 11).Seq | ||
ab5tract | m: (0, Int).grep(Int:D) ==> dd() | 12:59 | |
camelia | (0,).Seq | ||
ab5tract | We call those “smileys” :D | 13:01 | |
m: +(0,8,Int x 5, 9).grep(Int:U) ==> dd() | 13:03 | ||
camelia | Use of uninitialized value of type Int in string context. Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful. in block <unit> at <tmp> line 1 0 |
||
ab5tract | Dang autocorrect | ||
m: +(0,8,Int xx 5, 9).grep(Int:U) ==> dd() | |||
camelia | 0 | ||
ab5tract | Huh.. | ||
lizmat | And yet another Rakudo Weekly News hits the Net: rakudoweekly.blog/2023/12/04/2023-...g-this-ad/ | 13:05 | |
ab5tract | m: +(0,8,|(Int xx 5), 9).grep(Int:U) ==> dd() | 13:06 | |
camelia | 5 | ||
lizmat | m: say 5 ~~ Int:U | 13:08 | |
camelia | False | ||
lizmat | interesting... | 13:09 | |
also: afk for a few hours& | |||
nemokosch | Interesting? Seems okay to me | 13:12 | |
ab5tract | Probably a misread. | 13:31 | |
nemokosch | m: (0,8,|(Int xx 5), 9).grep(Int:U).elems.&dd | 13:35 | |
Raku eval | 5 | ||
19:23
meooow_ joined
19:25
gfldex_ joined,
meooow left
19:26
gfldex left
|
|||
fridge777 | is there a way to set multiple keys in a hash at once from a list with subscripts, something like %m{@keys} = $value ? | 20:37 | |
nemokosch | you might need to replicate the value on the right handside | 20:39 | |
other than that, I think it can work | |||
fridge777 | oh. yep that did it actually! thank you 😄 | 20:40 | |
nemokosch | 🍬 | 20:42 | |
22:15
foo63 left
|
|||
drudgesentinel | I'm trying to determine whether the following is valid use of trans sub translate-lines ($line) { $line.trans([ /<?after o>ne/, "two", "three", "four", "five", "six", "seven", /<?after e>ight/, "nine" ] => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); } I get expected values for simple cases, e.g. my $working-example = "onetwo3"; #o123 But for numbers that | 22:38 | |
'share' letters, raku seems to hang: my $gotcha = "twone"; # ctrl + C after 1 minutes | |||
The former example seems to imply the lookbehind works ok | 22:39 | ||
nemokosch | I've never heard about trans taking regular expressions | ||
gotta check the implementation though... | |||
drudgesentinel | Maybe I'm misintepreting the docs: Regexes work as keys I assume that the first set passed are the keys, and the 'replacements' the values in this context | 22:40 | |
I was previously just calling it like <one two three> => <1 2 3> but unfortunately my input had some shared letters in them | 22:41 | ||
nemokosch | you can be right, I wouldn't know offhand | ||
it really does hang | 22:46 | ||
m: 'twone'.trans((/<?after o>ne/, 'two') => (1, 2)) | |||
Raku eval | Failed while reading stream: Max execution time exceeded | ||
drudgesentinel | I suppose I could just split it into separate trans calls | 22:48 | |
be a shame though | |||
nemokosch | it can still just be a bug tbh | 22:49 | |
I think it cannot hurt opening an issue for it. It's not obvious at the very least why this needs to happen | 22:53 | ||
.vushu | @antononcube I made this module raku.land/zef:vushu/FEN::Grammar, maybe you could you this to render the chessboard the way you desire. | 22:58 | |
23:00
habere-et-disper joined
|
|||
habere-et-disper | Thinking about FOSDEM. Where to focus more: | 23:12 | |
At play with raku -- Unstructured learning via suggestibility, or | |||
At war with raku -- Combatting militaristic metaphors | |||
Otherwise I'll juggle both. Any preferences ? Thanks for your thoughts ! (y) :] |