.ohnowendigo | ab5tract: My requirements are this: given a list and a set of indices, I want to produce a new list where the values of the given indices have some transformation applied, and the rest are untouched. Say, to turn <a b c d> into <a B C d> (feeding in indices 1 and 2). Right now the only way I know how to do that is to clone the original list and mutate the clone, I was wondering if there was a more | 00:42 | |
elegant way | |||
ab5tract | m: my @a = <a b c d>; my $idx = set 1,2; my @b = @a.kv.map: -> $k, $v { $k (elem) $idx ?? $v.uc !! $v }; dd :@a, :@b | 00:48 | |
camelia | :a(["a", "b", "c", "d"]) :b(["a", "B", "C", "d"]) |
||
01:27
Manifest0 left
01:44
ACfromTX joined
04:09
lizmat_ joined
04:12
kjp left
04:13
lizmat left,
kjp joined
04:14
kjp left,
kjp joined
05:25
swaggboi left,
hudo__ left,
gfldex left,
discord-raku-bot left,
tbrowder left,
tonyo left
05:27
swaggboi joined,
hudo__ joined,
gfldex joined,
discord-raku-bot joined,
tbrowder joined,
tonyo joined
05:38
ACfromTX left
05:39
ACfromTX joined
|
|||
wambash | m: my $proc = [1,0,3]; $proc.map: {$ > 0 ?? ($, $-1) !! 0 } andthen [X,] $ andthen .say | 07:20 | |
Raku eval | ((1 0 3) (1 0 2) (0 0 3) (0 0 2)) | ||
07:39
lizmat_ left,
lizmat joined
07:54
human-blip left
07:55
human-blip joined,
human-blip left
07:56
human-blip joined
09:03
Manifest0 joined
09:14
dakkar joined
11:36
librasteve_ joined
|
|||
ab5tract | hmm, something must have gotten lost in translation with the discord bot | 17:21 | |
librasteve | $proc.map: {$_ > 0 ?? ($_, $_-1) !! 0 } andthen [X,] $_ andthen .say; | 17:38 | |
discord likes to drop the _ from $_ | 17:39 | ||
17:44
dakkar left
|
|||
it took me a bit of head scratching to work out what's going on ... but basically andthen takes the thing on it's left and puts it in the $_ on the rhs of the operator | 17:58 | ||
m: say [X,] ((1, 0), 0, (3, 2)); | 17:59 | ||
Raku eval | ((1 0 3) (1 0 2) (0 0 3) (0 0 2)) | ||
librasteve | where ((1, 0), 0, (3, 2)) is the result of the first .map clause | ||
so all the heavy lifting is done by [X,] ie reduce [] via cross metaoperator X with the , comma operator | 18:00 | ||
ab5tract | yeah, a pretty slick bit of code @wambash | 18:08 | |
I was trying to convert it to feed operators but haven't managed to so far | 18:18 | ||
wambash | m: my $proc = [1,0,3]; |$proc ==> map {$ > 0 ?? ($, $_-1) !! 0 } ==> reduce &[X] ==> say() | 18:24 | |
Raku eval | ((1 0 3) (1 0 2) (0 0 3) (0 0 2)) | ||
ab5tract | ah, nice! | 18:34 | |
librasteve | [1,0,3] ==> map {$_ > 0 ?? ($_, $_-1) !! 0 } ==> reduce &[X] ==> say() [fix the Discord ;-)] | ||
@wambash ... that's so cool - please can you explain the use of & in reduce &[X] ? | 18:35 | ||
ab5tract | it's necessary to "grab hold" of X | 18:36 | |
as &X won't refer to the meta-op | |||
ie it's the same as &[+] | |||
librasteve | m: say &+ | 18:40 | |
Raku eval | 3 | ||
librasteve | .oO | ||
wambash | m: say &[X] === &infix:<X> | 18:59 | |
Raku eval | True | ||
librasteve | m: [1,0,3] ==> map -> \n { n>0 ?? (n, n-1) !! 0 } ==> reduce &[X] ==> say() | 19:11 | |
Raku eval | ((1 0 3) (1 0 2) (0 0 3) (0 0 2)) | ||
librasteve | think I slightly prefer n over $_ in this example, otherwise perfect (ymmv) | 19:12 | |
ab5tract | librasteve: the bridge strikes again | 19:50 | |
m: say &+ | |||
camelia | ===SORRY!=== Error while compiling <tmp> Missing required term after infix at <tmp>:1 ------> say &+⏏<EOL> expecting any of: prefix term |
||
ab5tract | what am I missing there? | ||
librasteve | afaiui the syntax is &[+]($l,$r) | 19:59 | |
so the ampersand square brackets convert the operator into a function | 20:00 | ||
www.perplexity.ai/search/raku-synt...Uzr.fDQTEg | 20:15 | ||
perplexity knows this, but I cannot locate the relevant section of raku docs | |||
timemelon | I've come across this before, afaik it's not in the docs | 20:44 | |
ab5tract | This '3' is the result I'm asking about. What's missing from `&+` that causes the result of 3 usercontent.irccloud-cdn.com/file/.../image.png | 23:07 | |
librasteve: ^^ | |||
renormalist | Still struggling to "get" types, especially when to represent "nothing". So when I used "undef" in Perl, what do I use in Raku that works most universal? Nil? Any:U? (what's the difference), something else? | 23:11 | |
lizmat | renormalist: you might find this series of blog posts interesting: dev.to/lizmat/migrating-perl-to-raku-1c47 | 23:13 | |
renormalist | m'kay. I think I have read that series already with that question in mind. Maybe I'm just not getting it, though. | ||
lizmat | I actually realize I may not have covered that hmmm | 23:14 | |
renormalist | still assume I'm an idiot, I could really just holding it wrong. | 23:15 | |
lizmat | heh | ||
renormalist | I'm searching for something canaonical that works as "undef" in a list of Int and also in a list of Str. | ||
lizmat | Cool | ||
renormalist | [<a b c Nil e f>] | ||
lizmat | or Any | ||
renormalist | [1,2,3,Nil,5] | ||
so not Nil? | 23:16 | ||
lizmat | Nil indicates the absence of a value where there should be one | ||
m: my $a is default(42) = 666; say $a; $a = Nil; say $a | |||
camelia | 666 42 |
||
lizmat | assigning Nil reverts a container to its default state | ||
the default state of an array element is Any | 23:17 | ||
m: my @a; say @a[0] | |||
camelia | (Any) | ||
lizmat | m: my @a is default(42); say @a[0] | ||
camelia | 42 | ||
lizmat | you can change that with "is default" | ||
renormalist reads and thinks about it | |||
lizmat | m: my @a = 1,2,3, Nil, 42; dd @a | 23:18 | |
camelia | [1, 2, 3, Any, 42] | ||
lizmat | that's why you see Any there, and not Nil | ||
renormalist | so if I'm not directly on a variable $x, but instead thinking in lists with "unclear/undef" entries, would I place Any in them? | ||
ah, that's it, then | |||
renormalist needs a second to think about it | 23:19 | ||
lizmat | if you constrain an array to a type, then it would need to be that type | ||
m: my Int @a = 42; @a[1] = Any | |||
camelia | Type check failed for an element of @a; expected Int but got Any (Any) in block <unit> at <tmp> line 1 |
||
lizmat | m: my Int @a = 42; @a[1] = Int | 23:20 | |
camelia | ( no output ) | ||
lizmat | m: my Int @a = 42; @a[1] = Nil; dd @a | ||
camelia | Int = Array[Int].new(42, Int) | ||
renormalist | m: [1,2,3,Nil,5] | 23:21 | |
camelia | ( no output ) | ||
renormalist | m: my $l = [1,2,3,Nil,5]; dd $l; | 23:22 | |
camelia | $l = $[1, 2, 3, Any, 5] | ||
renormalist | m: my @l = [1,2,3,Nil,5]; dd @l; | ||
camelia | [1, 2, 3, Any, 5] | ||
renormalist | m: my Str @l = [1,2,3,Nil,5]; dd @l; | ||
camelia | Type check failed for an element of @l; expected Str but got Int (1) in block <unit> at <tmp> line 1 |
||
renormalist | m: my StrInt @l = [1,2,3,Nil,5]; dd @l; | ||
camelia | ===SORRY!=== Type 'StrInt' is not declared at <tmp>:1 ------> my StrInt⏏ @l = [1,2,3,Nil,5]; dd @l; Malformed my at <tmp>:1 ------> my⏏ StrInt @l = [1,2,3,Nil,5]; dd @l; |
||
renormalist | m: my IntStr @l = [1,2,3,Nil,5]; dd @l; | ||
camelia | Type check failed for an element of @l; expected IntStr but got Int (1) in block <unit> at <tmp> line 1 |
||
renormalist | :) damn | ||
m: my Int @l = [1,2,3,Nil,5]; dd @l; | 23:23 | ||
camelia | Type check failed for an element of @l; expected Int but got Any (Any) in block <unit> at <tmp> line 1 |
||
renormalist | m: my Int $nil is default(666); my Int @l = [1,2,3,$nil,5]; dd @l; | ||
camelia | Int = Array[Int].new(1, 2, 3, 666, 5) | ||
lizmat | m: dd Int ~~ IntStr | 23:24 | |
camelia | Bool::False | ||
lizmat | m: dd IntStr ~~ Int | ||
camelia | Bool::True | ||
lizmat | that's why the typecheck failed | ||
renormalist | so my numbers (Int) dont ~~ IntStr, but "1", "2" would? | 23:25 | |
m: dd "5".^name | 23:26 | ||
camelia | "Str" | ||
renormalist | m: dd IntStr.new("5").^name | ||
camelia | Too few positionals passed; expected 3 arguments but got 2 in block <unit> at <tmp> line 1 |
||
renormalist | m: IntStr $x is default("5"); dd $x.^name | ||
camelia | ===SORRY!=== Error while compiling <tmp> Two terms in a row at <tmp>:1 ------> IntStr⏏ $x is default("5"); dd $x.^name expecting any of: infix infix stopper statement end statement mod… |
||
renormalist | m: my IntStr $x is default("5"); dd $x.^name | 23:27 | |
camelia | ===SORRY!=== Error while compiling <tmp> Default value '5' will never bind to a variable of type IntStr at <tmp>:1 ------> my IntStr $x is default("5")⏏; dd $x.^name expecting any of: constraint |
||
renormalist | :) I need to type first in my terminal, before I confuse everything , brb :) | ||
lizmat | m: my IntStr $x is default(<5>); dd $x.^name | 23:30 | |
camelia | "IntStr" | ||
lizmat | m: my IntStr $x is default(<5>); dd $x | ||
camelia | IntStr $x = IntStr.new(5, "5") | ||
lizmat | <5> syntax creates allomorphs when possible | ||
m: dd <a b c 42 65.0 3i 137e0> | 23:31 | ||
camelia | ("a", "b", "c", IntStr.new(42, "42"), RatStr.new(65.0, "65.0"), ComplexStr.new(<0+3i>, "3i"), NumStr.new(137e0, "137e0")) | ||
renormalist | well, that touches another question I was about to ask today. What is an allomorph. Found earlier discussions on reddit with that. | ||
(If there is a short answer or URL) | |||
(I understand u cant explain the same stuff again forever) | |||
lizmat | docs.raku.org/syntax/Allomorph | 23:32 | |
hope you can work with that... | 23:33 | ||
renormalist | Ah, that's why a string 5 generated with <5> is in fact more than a Str, it is that 2-in-1 thing? While "5" is strictly only a Str. | ||
lizmat | yup | ||
lizmat is getting some shuteye& | |||
renormalist | yeah me too | ||
let's get some sleep | |||
thank you very much so far |