🦋 Welcome to Raku! raku.org/ | evalbot usage: 'p6: say 3;' or /msg camelia p6: ... | irclog: colabti.org/irclogger/irclogger_log/raku
Set by ChanServ on 14 October 2019.
kawaii I'm working on a basic !uptime command for a bot I'm writing, is there some more Perly way to do this instead of my clunky if/else? And possibly some hack to also switch between 'minute' and 'minutes' if $mins == 1 or not? :) www.irccloud.com/pastebin/H5G7rEEp/ 04:44
($start is defined way above at the... start... of my code) :) 04:47
elcaro kawaii: it can be done, but I think it would less readable 05:20
regarding pluralising words... i usually avoid golfing something like "you have { $n } thing{ 's' if $n ≠ 1 }"
I would rather write "you have $n { $n == 1 ?? 'thing' !! 'things' }" 05:21
even tho it's more typing
kawaii elcaro: oh both of those are nice solutions, thanks!
elcaro butbut with that caveat out of the way
tio.run/##PY5bCoMwEEX/XcVFhCQlBPvy...beJqxNP0BQ
i think it's less readable that what you've got tho. could also use `when` instead of `if` so your blocks align more nicely 05:22
but it's your code... do what you want... and "clever" code is usually ok in little scripts 05:23
kawaii elcaro: your solution there is actually quite concise, and legible, thank you very much :) 05:25
elcaro i had to rename your vars so I could build pairs out of them and just use the .key in a loop 05:27
it's a nice little trick
the toggle ensures we only ignore subsequent leading zero's 05:28
so the case where you have > 0 hours and seconds - but 0 mins - is handled fine
although it doesn't join the final 2 values with "and". 05:31
sortiz m: sub dur(:$s=0,:$m,:$h) { ($h ?? "$h hours, " !! '') ~ ($m ?? "$m minutes and " !! '') ~ "$s seconds" } say dur(:1s); say dur(:2s,:3m); say dur(:2m,:3h); # A basic approach with the same original semantic (Don't show zeros) 05:37
camelia 5===SORRY!5=== Error while compiling <tmp>
Strange text after block (missing semicolon or comma?)
at <tmp>:1
------> 3$m minutes and " !! '') ~ "$s seconds" }7⏏5 say dur(:1s); say dur(:2s,:3m); say dur
expecting any of:
sortiz m: sub dur(:$s=0,:$m,:$h) { ($h ?? "$h hours, " !! '') ~ ($m ?? "$m minutes and " !! '') ~ "$s seconds" }; say dur(:1s); say dur(:2s,:3m); say dur(:2m,:3h); # A basic approach with the same original semantic (Don't show zeros) 05:38
camelia 1 seconds
3 minutes and 2 seconds
3 hours, 2 minutes and 0 seconds
kawaii elcaro: doing that is way more trouble than it's worth imho, here's an example of that with an array of terms, almost illegible; `@items.&{ .head(*-1).&{ .join(", ") if $_ }, .tail }.join: " and "`
m: my @items = 'apple', 'peach', 'watermelon', 'lemon', 'lime'; say @items.&{ .head(*-1).&{ .join(", ") if $_ }, .tail }.join: " and "; 05:40
camelia apple, peach, watermelon, lemon and lime
moon-child kawaii: no oxford comma ;-; 06:16
sortiz: instead of ($h ?? "$h hours, " !! '') you can use ("$h hours, " if $h) 06:17
moon-child m: my @a = <apple peach watermelon lemon lime>; print "@a[0 .. *-2].map({"$_, "}).join()and @a[*-1]"; 06:20
camelia apple, peach, watermelon, lemon, and lime
sortiz moon-child++, you're right
moon-child oh, no, even easier 06:21
m: my @a = <apple peach watermelon lemon lime>; print "@a[0 .. *-2].join(', '), and @a[*-1]"
camelia apple, peach, watermelon, lemon, and lime
kawaii moon-child: it's 06:21, let's not have this debate now ;3
moon-child kawaii: why are you up so early?? 06:23
(though granted it's pretty late here...should sleep but eh)
kawaii I do my best work at night, I'll go to sleep shortly
sortiz Sweet dreams 06:25
elcaro moon-child: your code fails with a list of 1 :D 06:53
moon-child elcaro: tbf you probably wanna special-case both 1 and 2 anyway 06:55
because for 3 items: a, b, and c. For 2 items: a and b
elcaro yeah, I'm fairly certain there's a Lingua::* module for perl that does it
oh wait... there's already a Raku port: Lingua::Conjunction 06:56
jcarty Hello, is there a way to add something like error reporting to rakus grammars? I have a kind of large grammar and finding errors in some of the things it tries to read is challenging 08:00
andinus docs.raku.org/programs/01-debuggin...tribution) 08:04
docs link to Grammar::Debugger ^
kiti_nomad[m] Ask a very silly question, what does LWP mean 08:09
jcarty I've used Grammar::Debugger before in the past but that's more for actively debugging right? I was looking for a way to try and report something during it's regular use rather than just saying the parser failed. 08:10
moon-child kiti_nomad[m]: (l)ib(w)ww (p)erl?
moritz jcarty: something like Grammar::ErrorReporting? 08:11
shill warning: that's my module :-)
there's also Grammar::PrettyError
jcarty I think so I will take a look 08:12
moritz *and* Grammar::Message, because two modules aren't enough :D
also, my parsing book has a chapter on error reporting
kiti_nomad[m] right,thx 08:16
moon-child
jcarty moritz where would I find Grammar::Message. I found ErrorReporting and Pretty Error on Raku land. 08:19
andinus jcarty: it's in p6c ecosystem 08:21
github/ugexe/Perl6-ecosystems
moritz jcarty: modules.raku.org/search/?q=Grammar%3A%3AMessage 08:22
jcarty Ah thanks!
PimDaniel Coke: thank's for the subset you published for me yesterday after i leaved! 09:36
tellable6 2021-01-21T21:57:39Z #raku <[Coke]> pimdaniel gist.github.com/coke/53cf70ad3bc41...7b8e2b336f - there's a subset variant.
jmerelo releasable6: status
releasable6 jmerelo, Next release in ≈1 day and ≈9 hours. 1 blocker. 22 out of 74 commits logged
tellable6 2021-01-20T00:47:30Z #raku <tony-o> jmerelo another article/bit of info on zef eco - deathbyperl6.com/faq-zef-ecosystem/
releasable6 jmerelo, Details: gist.github.com/f8693ea70005474438...910241b81f
jmerelo .tell tony-o thanks!
tellable6 jmerelo, I'll pass your message to tony-o
PimDaniel I did not know the yet the subset keyword, so could not imagine it as a possibility. 09:37
tellable6: thank's i saw i in the #raku logs i whatch each morning. 09:42
tellable6 PimDaniel, I cannot recognize this command. See wiki for some examples: github.com/Raku/whateverable/wiki/Tellable
jcarty moritz Pretty errors is just what I was looking for initially thanks 10:52
PimDaniel Hi \o 12:10
andinus hi o/
PimDaniel I see that defined/not defined works.
like in Perl5
but Is is the good way to check definition of a variable? Or should a boolean check with if suffice? if $var {...} ???? 12:12
moritz that depends on what you want to check
El_Che $var.defined is a winner
moritz if you have an Int variable, for example if $var { ... } will be false both for undefined and 0
PimDaniel re 'my Int $i=0; say $i.defined' ==> True 12:14
re 'my Int $i=Nil; say $i.defined' => False
moritz: defined to 0 IS defined. 12:15
PimDaniel You meant : if i just check it with if. 12:17
elcaro yes, `if $x` just checks "truthiness", and Int 0 is "falsy", as is the empty list 12:18
m: my @a; say so @a;
camelia False
elcaro btw, `so` is the opposite of `not` 12:19
ie. a low precedence boolean coercer
PimDaniel re 'my Int $i=0; if $i {say "OK"} else {say "NO"}' ==> "NO"
Ok thank's , this is logical! 12:20
elcaro oh, there's also prefix `?` which is the opposite of prefix `!` 12:21
m: my @a; say ?@a; say !@a;
camelia False
True
PimDaniel Just to remind : what is the remplacement for : (statement(true)) ? do something : do other thing; in Raku. 13:13
PimDaniel i mean : (expression)? do this : do that; 13:14
guifa2 Statement ?? ifTrue !! ifFalse 13:15
PimDaniel guifa2: YESS! thank's : docs.raku.org/routine/%3F%3F%20!!
may submethod BUILD come with custom new method? 13:25
and if YES : must signatures be different? 13:27
moritz the two are pretty orthogonal 13:28
new() tends to call bless()
and bless in turn calss submethod BUILD
youc an use whatever arguments you like for new
but BUILD receives named arguments for each attribute to be initialized
PimDaniel if i have a custom new with a special signature : when i call it with my special caller : ok 13:31
I have a submethod BUILD with another signature and when i try to call it with another new caller : it complains that new sub has not good parameters. 13:33
what dit i not understand?
moritz show us the code (ideally condensed to the essential part) 13:34
PimDaniel pastebin.com/RgJhgRAB 13:35
PimDaniel note that method new alone works and method BUILD alone works too with good callers. 13:36
may be shoud i make a multi new instead... 13:38
caller for new instance : my $l = Line.new((33,22),(100,22), int-motif => 8364)
caller for BUILD which should be the default caller : my $l = Line.new(a => Point.new(33,22),b => Point.new(100,22),motif => 'X'); 13:39
PimDaniel I'v found the error : in this case, new MUST be a multi method. 13:48
... which seams logical since BUILD is a constructor too. 13:49
moritz it must only be a multi if you want the default to work (with named arguments) 13:50
PimDaniel Ok thank's moritz! 13:52
[Coke] yawns. 16:12
lucasb that is contagious 16:35
[Coke] muahaha 16:37
guifa2 Yeah in COVID times that's dangerous 17:00
tony-o patrickb: updating the faq now, ty 18:40
tellable6 2021-01-22T09:36:53Z #raku <jmerelo> tony-o thanks!
patrickb o/
tbrowder \o 21:38
tbrowder revisiting my prob yesterday with wrapping another sub (with help from dakkar, elcaro, and moritz), i think i may be able to use a Capture, if i read the docs correctly. 21:42
the wrapping sub: "sub foo($a, |c) { # rearrange the capture contents as necessary; my @res = wrapped-sub($a, c.Capture)}" 21:47
no, maybe: "my $c = c.Capture; @res = wrapped-sub($a, |$c);" 21:52
guifa2 Actually, c itself is a capture 22:05
So you only need @res = wrapped-sub($a, |c); 22:06
Tirifto[m] p6: map({ say $; $ }, map({ say $; $ }, (1, 2, 3))); 22:35
camelia 5===SORRY!5=== Error while compiling <tmp>
Name must begin with alphabetic character
at <tmp>:1
------> 3map({ say $7⏏5; $ }, map({ say $; $ }, (1, 2, 3)))
expecting any of:
infix
infix stopper
p…
tbrowder yes, but i need to modify it, so i took the hash "my %a = c.Hash;" modified the hash, and turned it into a new capture "my $d = %a.Capture" and fed it to my wrapped sub "my @res = wrapped $a, :$b, |$d" 22:36
Tirifto[m] (My apologies; I didn’t realise my client would mess with the text.)
tbrowder i have a simple test prog that works. now i have to try it in the real mccoy! 22:38
guifa2 tbrowder: ah, okay. If you are just using the hash values from the capture, then you can skip a step and just slip in the hash, e.g. wrapped $a, |%a 22:41
tbrowder ok, cool, thnx
Tirifto[m] p6: map({ say $_; $_ }, map({ say $_; $_ }, (1, 2, 3))); 22:53
camelia 1
1
2
2
3
3
Tirifto[m] ^ Just wanted to say I found this pretty curious. 😸
Tirifto[m] I wonder how the order was determined here? Does the inner function only get mapped to an element when the outer function asks for an element first? 22:56
guifa2 AFAIK map isn't lazy, but the order holds up when doing it a different way: 23:05
sub slow($x) { sleep 1; $x }; (1,2,3).map({ .say; slow $_ }).map( *.say );
evalable6 1
1
2
2
3
3
guifa2 And is different from 23:07
m: sub slow($x) { sleep 1; $x }; (1,2,3)>>.&{ .say; slow $_ }>>.&{ .say }; 23:08
camelia 1
2
3
1
2
3
guifa2 Tirifto: actually, the docs state that .map on a List *is* lazy 23:10
Tirifto[m] guifa2: Ohhh, okay, I see that part now. 23:11
It says ‘lazily’, so I didn’t see it when I lazily searched for ‘lazy’. :-)
guifa2 Tirifto: same actually :-) 23:12
(and hey, I just learned one of the small differences between .map and >>. )
Tirifto[m] I only remember that ». doesn’t provide a reliable order of function application. So technically it could also print «3␤1␤2␤1␤2␤3␤», right? 23:14
guifa2 Yeah. Although presently it does always execute in order 23:15
Tirifto[m] Hmm… so is that likely the reason the order goes like that, guifa2? Because ». is not lazy like map is when it comes to Lists? 23:23
guifa2 Tirifto: Yeah, I believe so. Note the difference too: 23:26
say (1,2,3).map({$_}).WHAT; say (1,2,3)>>.&{$_}.WHAT
evalable6 (Seq)
(List)
guifa2 also excuse me while I rinse my keyboard off after that line noise mess
Tirifto[m] Have a good rinse! 23:27
Tirifto[m] And I see… My understanding is that sequences are inherently more lazy, so that would fit in. :-) 23:29
Well, back to imperative style! I guess that was the right choice in the first place, given my care for side effects. 23:31
guifa2 m: sub slow($x) { sleep 1; $x }; (1,2,3).map({ .say; slow $_ }).eager.map( *.say ); 23:34
camelia 1
2
3
1
2
3
guifa2 Tirifto: ^^ if you want to use the functional style still 23:35
Tirifto[m] Ohh, eager! 23:41
guifa2: Thanks! I’ll see where it would fit in. Might end up with a hybrid approach. That feels like a good way to go with Raku in general. 23:44