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:31
Manifest0 left
01:31
habere-et-disper left
02:13
teatwo joined
02:16
teatime left
03:44
Heptite joined
04:10
Heptite left
08:45
teatwo left,
teatwo joined
09:00
Manifest0 joined
09:04
razetime joined
11:24
ab5tract joined
11:39
ChanServ sets mode: -o lizmat
12:03
razetime left
14:13
tea3po joined
14:16
teatwo left
14:18
tea3po left,
tea3po joined
15:18
tea3po left,
tea3po joined
17:56
Heptite joined
20:52
avuserow_ joined
|
|||
avuserow_ | Hey folks, can someone point me to what the semicolon in the parens is doing in the following code? I know it's making a list, somehow, but not really sure why it's working (and why a comma does not): | 20:55 | |
m: my $a = True; my $b = False; dd ("z" if $a; "ro" if $b); | |||
camelia | ("z",) | ||
avuserow_ | m: my $a = True; my $b = True; dd ("z" if $a; "ro" if $b); | ||
camelia | ("z", "ro") | ||
20:56
avuserow_ is now known as avuserow
|
|||
Nemokosch | I think this is a semicolon-separated list there | 20:58 | |
m: my $a = True; my $b = True; dd ("z" if $a; "ro", "mania" if $b); | |||
Raku eval | ("z", ("ro", "mania")) | ||
Nemokosch | docs.raku.org/language/list.html look up the word "semicolon" | 21:03 | |
gfldex | avuserow: it's creating a 2-dimensional list. In your first example, the 2nd value happends not to be created because the statement form of if does suppress it. | 21:06 | |
m: my $a = True; my $b = False; dd("z" if $a; "ro" if $b); | |||
camelia | ===SORRY!=== Error while compiling <tmp> Unable to parse expression in argument list; couldn't find final ')' (corresponding starter was at line 1) at <tmp>:1 ------> my $a = True; my $b = False; dd("z" if ā$a; "ro" if $b); |
||
Nemokosch | I'd think the second value was Empty in that case | 21:07 | |
m: dd ("z"; Empty) | |||
Raku eval | ("z",) | ||
gfldex | please note that `dd()` is not the same as `dd ()`. In the latter form, the space turns () into a list-literal (if there is a , or ; inside). | 21:08 | |
avuserow | m: sub mode(:$a, :$b) {my $mode = join ',', ("ro" if $a; "z" if $b); $mode = ":$mode" if $mode; return $mode // ''} say mode(:a, :!b) # closer to my actual use-case, maybe there's a better way to write this? | 21:10 | |
camelia | ===SORRY!=== Error while compiling <tmp> Strange text after block (missing semicolon or comma?) at <tmp>:1 ------> = ":$mode" if $mode; return $mode // ''}ā say mode(:a, :!b) # closer to my actual expecting any of: ā¦ |
||
avuserow | m: sub mode(:$a, :$b) {my $mode = join ',', ("ro" if $a; "z" if $b); $mode = ":$mode" if $mode; return $mode // ''}; say mode(:a, :!b) # closer to my actual use-case, maybe there's a better way to write this? | 21:11 | |
camelia | :ro | ||
avuserow | maybe my brain is just making this too complex and I should just use an if/elsif/else block :shrug: | ||
gfldex | are mode ro and z mutual exclusive? | 21:12 | |
m: say Empty.defined; | 21:15 | ||
camelia | False | ||
gfldex | m: m: sub mode(:$a, :$b) {my $mode = join ',', ("ro" if $a andthen "z" if $b); $mode = ":$mode" if $mode; return $mode // ''}; say mode(:a, :!b) | ||
camelia | ===SORRY!=== Error while compiling <tmp> Missing semicolon at <tmp>:1 ------> ode = join ',', ("ro" if $a andthen "z" āif $b); $mode = ":$mode" if $mode; retur |
||
gfldex | m: say Empty.Bool; | 21:16 | |
camelia | False | ||
Nemokosch | what is the supposed output? | 21:19 | |
avuserow | no, output is one of `:ro` `:z:` :ro,z` or empty `` | 21:22 | |
no, output is one of `:ro` `:z` :ro,z` or empty `` | |||
I don't think I'm going to end up with a third option here so I'll just write it as a boring if/elsif/else block | 21:23 | ||
Nemokosch | m: (True, False) Z&& <ro z> andthen .say | 21:28 | |
Raku eval | (ro False) | ||
Nemokosch | m: (True, False) Z&& <ro z> andthen .grep(&so) # drop falsy values | 21:29 | |
Raku eval | |||
Nemokosch | forgot to print | ||
m: (True, False) Z&& <ro z> andthen .grep(&so).say # drop falsy values | |||
Raku eval | (ro) | ||
Nemokosch | m: (True, True) Z&& <ro z> andthen .grep(&so).join(',').say | 21:32 | |
Raku eval | ro,z | ||
gfldex | m: sub mode(:$a, :$b) { if ("ro" if $a) ~ (",z" if $b) -> $_ { ":$_" } else { '' } }; dd mode(:a, :!b); dd mode(:a, :b); dd mode(); | 21:33 | |
camelia | ":ro" ":ro,z" "" |
||
gfldex | avuserow: ^^^ that's shorter but I'm not sure if it is better. | ||
Nemokosch | I like it if that counts š | 21:34 | |
avuserow | I learned I can specify `:rw` if $a is false, so I can simplify the edge cases into approximately this: | 21:41 | |
m: sub mode(:$a, :$b) {my $mode = $a ?? ':rw' !! ':ro'; $mode ~= ':z' if $b; return $mode}; say mode(:a, :!b); say mode(:!a, :b) | 21:42 | ||
camelia | :rw :ro:z |
||
avuserow | m: sub mode(:$a, :$b) {my $mode = $a ?? ':rw' !! ':ro'; $mode ~= ',z' if $b; return $mode}; say mode(:a, :!b); say mode(:!a, :b) | ||
camelia | :rw :ro,z |
||
gfldex | m: sub prepend(Str:D $s, Str:D $prefix) { "$prefix$s" }; sub mode(:$a, :$b) { ("ro" if $a) ~ (",z" if $b) andthen .&prepend(':') // '' }; dd mode(:a, :!b); dd mode(:a, :b); dd mode(); | 21:43 | |
camelia | ":ro" ":ro,z" ":" |
||
gfldex | I wish we had Str.prepend . | ||
avuserow | m: my $s = "def"; $s = $s R~ "abc"; say $s # maybe? | 21:44 | |
camelia | abcdef | ||
gfldex | Or an infix prepend operator with andthen semantics for Str. | ||
Nemokosch | what's the matter with R~ ? | ||
gfldex | m: sub mode(:$a, :$b) { ("ro" if $a) ~ (",z" if $b) andthen .fmt(':%s') // '' }; dd mode(:a, :!b); dd mode(:a, :b); dd mode(); | 21:49 | |
camelia | ":ro" ":ro,z" ":" |
||
gfldex | Silly me, we got .fmt on Str. | ||
avuserow | hmm, looks like you have a single colon still in the third case? | 21:55 | |
Nemokosch | m: dd Empty ~ Empty | 21:56 | |
Raku eval | "" | ||
Nemokosch | it is defined indeed so andthen will execute | ||
gfldex | m: sub mode(:$a, :$b --> Str()) { ("ro" if $a) ~ (",z" if $b) andthen ( .fmt(':%s') if .so ) }; dd mode(:a, :!b); dd mode(:a, :b); dd mode(); | 21:57 | |
camelia | ":ro" ":ro,z" "" |
||
gfldex | avuserow: ^^^ | ||
I didn't know --> Str() actually works. | 21:58 | ||
That turns Empty into "". | |||
And is an ENODOC. :-/ | |||
avuserow | nice, thanks for indulging me in this :+1: | 22:00 | |
gfldex | One blogpost, 2 ENODOC issues and giving lizmat a good reason for a quick PR. My work is done for today. :-> | 22:15 | |
23:22
Manifest0 left
|