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.
masukomi is this one long enough? stackoverflow.com/questions/337887...6#66056086 00:35
. 00:37
separately
i keep getting `expected Associative[Hash] but got Hash` when calling this method `our sub insert-pr(Hash %pr_details, DB::SQLite $db){`
i had something similar earlier with `expected Positional[Array] but got Array` in another method. Anyone know what's going on or what the magic incantation is to make it not expect something I didn't tell it to expect?
guifa_ masukomi do you intend for pr_details to be a hash of hashes? 00:40
masukomi i do not. 00:41
guifa_ Okay, there's the issue :-) You've se %pr_details to be an associative whose value type is a Hash (so, a hash of hashes, in effect) 00:42
either change to
Anton Antonov <@526609611680120842> Yes, it is long.
guifa_ our sub insert-pr(Hash $pr_details … )
or to keep the % sigil
Anton Antonov <@526609611680120842> Yes, it is long enough. 00:43
guifa_ our sub inserst-pr(%pr_details where Hash … ) 00:44
But I'm going to guess what you really care about is that it's Associative — that is, a set of key values. In that case, the fact that it has a % sigil takes care of it:
masukomi a) thank you b) I would have never guessed the 2nd syntax. I'd like to keep the `%` because then it'll be more obvious in the method that it's a Hash wherever referenced. 00:45
guifa_ m: sub foo(%a) { say "%a is a hash" }; my %b = <a A b B>; foo %b 00:46
camelia %a is a hash
guifa_ m: sub foo(%a) { say "%a is a hash" }; my $b = 5; foo $b
camelia Type check failed in binding to parameter '%a'; expected Associative but got Int (5)
in sub foo at <tmp> line 1
in block <unit> at <tmp> line 1
guifa_ Also, the error with Position[Array] but got Array is the same idea. `Array @foo` is a list-y thing (officially Positional) that contains only Array objects. To force it to be an Array, `Array $foo` or `@foo is Array`. But if you don't care whether it's an Array, or a List, or anything else that contains items in a sequence, just use `@foo` and it'll accept any of them 00:53
masukomi thanks. 00:56
deoac Lately, I've been writing code like `$x = foo $x with $x`. Is there a more concise way to write this? 02:35
`foo` is a sub, not a method
Nemokosch deoac: what about `$_ = .&foo with $x` ? 05:45
apparently .= also works with the ampersand 05:49
`.=&foo with $x` can work (mind that there is no space between = and & in this case, otherwise it doesn't parse for some reason) 05:50
Nahita ```raku 06:55
# new op
sub infix:<?=>(\op1, &op2) { op1 = op2(op1) with op1 };
# sample
my ($x, $y) = 4;
my &f = * - 7;
$x ?= &f;
$y ?= &f;
say $x, " ", $y; # -3 (Any)
```
Nemokosch how do I search a sequence of items in a list? 09:10
lizmat you mean finding (3,4,5) in (1,2,3,4,5,6) ? 09:12
Nemokosch yes 09:14
Nahita $seq.rotor($needle => -$needle .succ).grep(* eqv $needle).so 09:18
Nemokosch thank you, this will work with first 09:23
lizmat m: my @a = 1..10; my @b = 4,5,6; say @a.grep(@b[0],:k).map({ my $slice := @a[$_ .. $_ + @b.end]; $slice if $slice eqv @b.List }) 09:25
camelia ((4 5 6))
lizmat this works only if the source is a List / Array 09:26
the .rotor approach is more general, but probably also a lot more CPU intensive, as it will create lists for *each* element in the source, rather than only when the first element matches in my conceptual version 09:27
Nemokosch I didn't think this would be a problem but... I actually don't want the first match. I want the position of possibly all matches 09:33
lizmat m: my @a = 1..10; my @b = 4,5,6; say @a.grep(@b[0],:k).grep({ @a[$_ .. $_ + @b.end] eqv @b.List }) 09:34
camelia (3)
lizmat basically, grep on the first element first, get the positions, and then further grep on matches, and wind up with the positions 09:35
Nemokosch my $pos = $list.rotor($latin-name-list => -$latin-name-list .succ).grep(* eqv $latin-name-list, :k) 09:37
this did work, by the way, but yes it did kinda melt the CPU
lizmat and yet another Rakudo Weekly News hits the Net: rakudoweekly.blog/2022/10/03/2022-...g-to-core/ 13:20
deoac Nahita: Thanks, that's a good idea. 13:27
Rog Wouldn’t this go out of bounds? 18:08
Say the list’s length is 2
Take two, advance -3
Nemokosch I think that's impossible with rotor, unless you set some option
Rog m: (1..10).rotor(2 => -3)
Nemokosch hahaha
gotcha - the code is whitespace sensitive
-$n .succ is -$n + 1 18:10
-$n.succ is -$n - 1
Rog Ugh
That feels… kinda bad
Nemokosch I don't know how this parses tbh
but apparently there is this difference
Rog It feels inconsistent
I mean if method call binds more tightly than numeric negation, I don’t think whitespace alone ought to change that
Nemokosch something is definitely very interesting with the parsing 18:12
Rog It almost feels intentional but somehow I think it isn’t?
Gosh I really don’t know
Nemokosch could it be that it's somehow not a method call when the whitespaces are present? 18:14
if I add whitespaces between the sign and the sigil, that makes no difference
lizmat yeah, method calls bind more tightly than prefix op 18:18
also over the years been a long discussion, and that's where we wound up with
the whitespace version has a lower priority, but that was really just a (nice) side-effect 18:19
just like natural languages, it depends where you put your comma's :-) 18:20
and yes, I've been bitten by that myself :-)
Nemokosch how is it implemented? I see that 1-$num .succ parses as 1-($num.succ); what's the rule? 18:25
lizmat src/Perl6/Grammar.nqp contains the implementation :-) I have no idea where that is exactly implemented, without looking at it in depth 19:01