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.
01:13 MasterDuke left 01:48 habere-et-disper left 04:15 swaggboi left 04:28 swaggboi joined 05:41 vlad joined 06:39 vlad left 06:57 vlad joined
ab5tract I personally think the ergonomics around getting the values out of matches are a bit annoying, but here’s an option that feels mostly fine to me: 10:47
m: dd my Int() @g = @(“11 22” ~~ m/(\d\d)\s(\d\d)/) 10:48
camelia Int(Any) @g = Array[Int(Any)].new(11, 22)
ab5tract Or if you want them as strings, just coerce using Str() instead 10:49
bobby_jim_birdrock: I hope it helps ^^ 10:50
Of course it can be extended to assigning into scalars as well: 10:51
m: dd my Int() ($x, $y) = @(“11 22” ~~ m/(\d\d)\s(\d\d)/)
camelia (11, 22)
11:19 lizmat left 11:22 lizmat joined
librasteve here's my take on this: 12:57
m: my $hex = "0810"; dd $hex ~~ /(\d\d)(\d\d)/; 12:58
Raku eval Match.new(:orig("0810"), :from(0), :pos(4), :list((Match.new(:orig("0810"), :from(0), :pos(2)), Match.new(:orig("0810"), :from(2), :pos(4)))))
librasteve ^^ using dd (data dumper) here to see what I have got ... OK looks like a nested Match, but Match is Positional, so I can maybe flatten it with '|'
m: my $hex = "0810"; dd |($hex ~~ /(\d\d)(\d\d)/); 12:59
Raku eval Match.new(:orig("0810"), :from(0), :pos(2)) Match.new(:orig("0810"), :from(2), :pos(4))
librasteve ^^ that's better now I have a List of two Matches, can use the my () dyntax to destructure on the LHS of an assignement
m: my $hex = "0810"; my ( @a ) = |($hex ~~ /(\d\d)(\d\d)/); dd @a; 13:00
Raku eval Array @a = [Match.new(:orig("0810"), :from(0), :pos(2)), Match.new(:orig("0810"), :from(2), :pos(4))]
librasteve ok I can load @a ok, but I want Str on the lhs... 13:01
m: my $hex = "0810"; my ( Str() $row, Str() $col ) = |($hex ~~ /(\d\d)(\d\d)/); say $row, $col;
Raku eval 0810
librasteve m: my $hex = "0810"; my ( Str() $row, Str() $col ) = |($hex ~~ /(\d\d)(\d\d)/); say "row is $row, col is $col"; 13:02
Raku eval row is 08, col is 10
librasteve ^^ since the my () has the same syntax as a sub call, I can use types - in this case Str() is a coercion type that calls .Str on the value passed in 13:03
13:06 vlad left
m: my (Str() %h) = <row col> Z=> $hex ~~ /(\d\d)(\d\d)/; say %h; 13:08
Raku eval Exit code: 1 ===SORRY!=== Error while compiling /home/glot/main.raku Variable '$hex' is not declared at /home/glot/main.raku:1 ------> my (Str() %h) = <row col> Z=> ⏏$hex ~~ /(\d\d)(\d\d)/; say %h;
librasteve m: my $hex = "0810"; my (Str() %h) = <row col> Z=> $hex ~~ /(\d\d)(\d\d)/; say %h; 13:09
Raku eval {col => 10, row => 08}
librasteve and coercion types can be used with %hashes and the Z=> zips the rhs into Pairs ;-) 13:10
gfldex m: class Foo { constant Parser = regex { $<int>=(\d+) $<str>=(\w+) } has Int() $.int; has Str() $.str; } my Foo() $foo .=new: |('123 abc' ~~ Foo::Parser).hash; dd $foo; 13:19
Raku eval Foo $foo = Foo.new(int => 12, str => "3")
gfldex @librasteve You don't even need coersion types, if the target is a class. 13:20
librasteve he he 13:23
gfldex m: ``` class Foo { constant Parser = regex { $<int>=(\d+) $<str>=(\w+) } has Int() $.int; has Str() $.str; method from-str(Str() $s) { self.new: |($s ~~ Parser).hash; } } my Foo $foo .=from-str: '123 abc'; dd $foo; ````
Raku eval Exit code: 1 ===SORRY!=== Error while compiling /home/glot/main.raku Bogus statement at /home/glot/main.raku:12 ------> dd $foo;⏏<EOL> expecting any of: prefix term
gfldex Less boilerplaty version. 13:24
librasteve that Foo::Parser constant thing is new to me ... til
13:39 vlad joined
ab5tract gfldex: but your class is using coercion types :) 14:28
gfldex I have not given up hope for typed matches. 14:32
14:49 vlad_ joined 14:53 vlad left 15:47 ACfromTX left 16:37 vlad_ left 16:39 vlad joined 18:00 ACfromTX joined 18:21 gfldex left 18:25 gfldex joined
librasteve ``` my regex number { \S+ #grab chars <?{ +"$/" ~~ Real }> #assert coerces via '+' to Real } 18:30
can just do lookahead assertion for “typed matches”
18:32 vlad_ joined 18:36 vlad left 20:17 vlad_ left 20:54 MasterDuke joined 21:09 teatime left
bobby_jim_birdrock Wow you guys are fun! I especially like the flattening operator. And... I didn't know we had a Raku bot... 23:36
To exit type 'exit' or '^D' [0] > my $hex = "0810" 0810 ... [7] > my ( Str() $a, Str() $b ) = |($hex ~~ /(\d\d)(\d\d)/); (08 10) [8] > $a 08 [9] > $b 10 [10] > 23:39