Re: can u suggest a beginner book for perl6?

2021-09-30 Thread JJ Merelo
May I suggest my own "Perl 6 Quick Syntax Reference"?
https://www.apress.com/gp/book/9781484249550

El mié, 29 sept 2021 a las 14:26, Elizabeth Mattijsen ()
escribió:

> Perhaps Think Raku: https://greenteapress.com/wp/think-perl-6/ ?
>
> > On 29 Sep 2021, at 14:18, Walt CH  wrote:
> >
> > I have some ruby programming experience, and some JS.
> > can you suggest a book for newbie of perl6?
> >
> > Thanks
>
>

-- 
JJ


Re: Order of multi sub definitions can resolve dispatch ambiguity

2021-09-30 Thread Joseph Brenner
Joseph Brenner  wrote:

> And now Jonathan Worthington has announced that the new
> Multi-dispatch mechanism is done... any bets on whether
> any of this behavior will change?

I don't see any change in this behavior using a fresh build from github.


Re: Order of multi sub definitions can resolve dispatch ambiguity

2021-09-30 Thread Joseph Brenner
Like I was saying:

> If the two [multis] are defined in different modules I suspect you
> could get some strange behavior that would be hard to debug.
> It's not always immediately obvious in what order two things were
> defined.

Here's an example, scattered across four files, which
I've also pushed out to github:

  https://github.com/doomvox/raku-study/blob/main/md_exp/

The deal here is one script treats "godzilla" as a Hero,
the other as a Monster, and the only difference is the
order of two "use" lines:

 ---
md_exp/lib/Speak/Monster.rakumod:

module Speak::Monster {
my @monsters  = < godzilla  gammera   ghidora   golem>;
subset Monster of Str where { $_ eq any( @monsters ) };
multi sub speak (Monster $name) is export {
say "The monster, $name roars!";
}
}


 ---
md_exp/lib/Speak/Hero.rakumod:

module Speak::Hero {
my @heroes= < godzilla  beowulf   ultraman  inframan >;
subset Hero of Str where { $_ eq any( @heroes ) };
multi sub speak (Hero $name) is export {
say "The hero, $name shouts!";
}
}

 ---
md_exp/bin/multidispatch_overlapping_subsets_use_order_dependency-a.raku

use lib $*PROGRAM.parent(2).add("lib");
{
  say "a: Hero Monster trial run";
  use Speak::Hero;
  use Speak::Monster;
  speak('godzilla');## The hero, godzilla shouts!
}


 ---
md_exp/bin/multidispatch_overlapping_subsets_use_order_dependency-b.raku

use lib $*PROGRAM.parent(2).add("lib");
{
  say "a: Monster Hero trial run";
  use Speak::Monster;
  use Speak::Hero;
  speak('godzilla');## The monster, godzilla roars!
}
 ---

But the situation is even worse than this: the
order-of-definition doesn't depend on the order of the
use lines in the block of code in front of you:
in a large code base you might have use lines scattered
in different places, and the order-of-definition is the
order in which raku first sees the use lines.


Re: Order of multi sub definitions can resolve dispatch ambiguity

2021-09-30 Thread Joseph Brenner
I'm not entirely sure what you're getting at with that example.
I was particularly interested in cases where there's some
ambiguity in multi-dispatch for particular values, where there's
some overlapping coverage between the prototypes.

Just for the hell of it, I'll stick another example at
the bottom of this message using numeric ranges this
time.

And it isn't exactly the way I *want* it to work, it's the way I
thought it was documented to work. I think I can see problems
with doing it either way:

If you use order-of-definition to resolve some of the difficult
cases, you have a situation where it can matter what order raku
has encountered the multi definitions.  Twiddling the order of
"use" lines could produce unexpected behavior... and there's some
risk of action-at-a-distance confusion: a new "use" line added in
a remote part of the code might effect the behavior of code you
thought you had working.

But if you give up on using order-of-definition, and the "is default"
tie-breaker is the only hint you have to resolve ambiguities,
what do you do if you've got ambiguity for different values in
*three* different cases?  If you make A "is default" to resolve
an ambiguity with B, what do you do about an ambiguity between B
and C?  You could apply "is default" to C, but that's no good if
you also had ambiguities between A and C.

(I think in practice I'd contrive some more complicated
subset/where checks that resolve the ambiguities before handing
it off to multi-dispatch...)

By the way, I don't see any reference to these sort of
things here:

   https://raku.org/archive/doc/design/apo/A12.html

I also don't see anything that exercises these cases here:

  roast/S12-subset/subtypes.t
  roast/S12-subset/multi-dispatch.t

And now Jonathan Worthington has announced that the new
Multi-dispatch mechanism is done... any bets on whether
any of this behavior will change?


As promised above, yet another example, this one using
overlapping numeric ranges via where clauses.  Here the
order of the multis resolves the ambiguous case "4":

  {
multi sub check_range ( Int $n where {$_ > 3} ) {
return "over 3";
};
multi sub check_range ( Int $n where {$_ < 5} ) {
return "under 5";
};
say "2: ", check_range(2);  # 2: under 5
say "7: ", check_range(7);  # 7: over 3
say "4: ", check_range(4);  # 4: over 3
  }

  say "===";
  {
multi sub check_range ( Int $n where {$_ < 5} ) {
return "under 5";
};
multi sub check_range ( Int $n where {$_ > 3} ) {
return "over 3";
};
say "2: ", check_range(2);  # 2: under 5
say "7: ", check_range(7);  # 7: over 3
say "4: ", check_range(4);  # 4: under 5
  }



On 9/25/21, Brad Gilbert  wrote:
> On Sat, Sep 25, 2021 at 2:30 PM Joseph Brenner  wrote:
>
>> > Since subsets are just code it would be difficult to impossible
>> > to determine all of the ones that can match without actually
>> > running them all. This would slow down every call that uses
>> > multiple subsets.
>>
>> I can see how there would be a big performance hit, but I haven't
>> yet turned up any reference to this behavior in the docs.  I
>> don't see anything to clue you in that subsets are an exception,
>> and I don't think there's anything about
>> order-of-definition-of-sub being a tie-breaker under any
>> circumstances.
>>
>
> The docs are not the definitive source of truth.
> They are at best an approximation.
>
>> By most narrow candidate, it means by type.
>>
>> If you'd said that to me, I'd still have expected it to work the
>> way that I did, because the way I look at it I'm creating new
>> types via subset.
>>
>
> By type I mean something that can be instantiated or subclassed.
> Subsets cannot do either of those two things.
> You can create a subset of a subset, but that is not the same thing.
>
>
>> > All subsets "of" the same type are sorted by the order they are
>> > written.
>>
>> Even if I'd figured that two subsets of Str were going to treated as Str,
>> I'd have expected an "Ambiguous call" error.
>>
>> If the two subsets are defined in different modules I suspect you
>> could get some strange behavior that would be hard to debug.
>> It's not always immediately obvious in what order two things were
>> defined.
>>
>
> Doing what you want would make using subsets with multis much less useful.
>
> multi factorial ( 0 --> 1 ){}
> multi factorial ( 1 --> 1 ){}
> multi factorial ( UInt \n ){ factorial(n - 1) * n }
>
> say factorial( 1 );
> # ERROR: both UInt and 1 subsets match.
>
> You could modify the general case, but that is tedious and error prone.
>
> multi factorial ( Int \n where {$_ >= 0 && $_ != 0 && $_ != 1} ){
> factorial(n - 1) * n }
>
> (The reason I didn't just do $_ >= 2 is that if another multi is added it
> should be dealt with in the same way as 0 and 1)
>
>
>> On 9/24/21, Brad Gilbert  wrote:
>> > This is intended behavior.
>> >
>> > Since subsets are just code it would be difficult