recent perl6 book

2020-07-20 Thread Warren Pang
Greetings,

Would you suggest a recent release of perl6 book?
I took a look here:
https://perl6book.com/

They seem out of date, most were published before 2018.

Thank you.


Re: doing an inner join via cross-product

2020-07-20 Thread Bruce Gray



> On Jul 19, 2020, at 3:02 PM, Joseph Brenner  wrote:
> 
> I was thinking about the cross-product operator the other day,
> and I was wondering if there might be a convenient way of
> filtering the resulting cartesian product to do something like a
> database inner join:
> 
>my @level  = ( godzilla => 9 ,gremlin => 3, hanuman => 5 );
>my @origin = ( godzilla => 'jp',  tingler => 'us',  hanuman => 'il' );
> 
>my @results = ( @level X @origin ).grep({ $_[0].keys eq $_[1].keys });
>say @results;  # ((godzilla => 6 godzilla => jp) (hanuman => 5
> hanuman => il))

—snip--

This is a neat idea, but it will scale very poorly.
If you had 3 arrays instead of 2, and just 20 pairs in each array instead of 3,
Raku would generate 8000 pairs, which would then be filtered down to 20 at most.

This should behave better, and be almost as concise:
my %monster = (|@level, |@origin)
.classify( {.key}, :as{.value} )
.grep({ .value.elems == 2 }); # The `grep` filters to become like an inner 
join.

say .key.fmt('%7s => '), .value.raku for %monster.sort;

printf "%12s: level: %-2d origin: %3s\n", .key, |.value for %monster.sort;


> Is there some neater way of doing this that I'm missing?

Your line:
my %joined2 =| @results.map({ $_[0].keys => .map({ .values }).flat }); 
can be re-written as:
my %joined2 = @results.map: { .[0].key => .list».value };



BTW, I initially missed that “inner join” was an important factor, and locked 
in on the use of "parallel arrays" (which I spoke of as a Code Smell at TPC 
this year)
So, I wrote this:


sub parallel_arrays_to_HoH ( *@pairs ) {
my %r;
for @pairs -> ( :key($attribute_name), :value(@main_key_attr_value_pairs) ) 
{
%r{.key}{$attribute_name} = .valuefor @main_key_attr_value_pairs;
}

for %r.values <-> %subhash {
%subhash{$_} //= Any for @pairs».key;
}

return %r;
}

, which produces outer-joined HashOfHashes. It is not as succinct, but is 
encapsulated and reusable.
The HoH is easily filtered to produce a inner join.

Usage example:

my @level  = godzilla => 9 ,gremlin => 3, hanuman => 5;
my @origin = godzilla => 'jp',  tingler => 'us',  hanuman => 'il';

my %outer_joined_monster = parallel_arrays_to_HoH( (:@level), (:@origin) ); # 
Those parens are required.

say .key.fmt('%7s => '), .value.raku for %outer_joined_monster.sort;

my %inner_joined_monsters = %outer_joined_monster.grep:
*.value.values».defined.all.so;

printf "%12s: level: %-2d origin: %3s\n", .key, .value for 
%inner_joined_monsters.sort;

Produces:
godzilla => ${:level(9), :origin("jp")}
gremlin => ${:level(3), :origin(Any)}
hanuman => ${:level(5), :origin("il")}
tingler => ${:level(Any), :origin("us")}
godzilla: level: 9  origin:  jp
 hanuman: level: 5  origin:  il

— 
Hope this helps,
Bruce Gray (Util of PerlMonks)


Re: I cannot install any lib with Zef

2020-07-20 Thread Aureliano Guedes
Thanks, I checked it out.
now it is working

But now I'm getting issues to install  zef install Jupyter::Kernel, the
issue itself is on Digest::SHA256::Native:ver<0.03> installation.

$ zef install "Digest::SHA256::Native:ver<0.03>" --force-build --force-test

> ===> Searching for: Digest::SHA256::Native:ver<0.03>
> ===> Building: Digest::SHA256::Native:ver<0.03>
> [Digest::SHA256::Native] /usr/bin/ld: cannot find -lffi
> [Digest::SHA256::Native] /usr/bin/ld: cannot find -ltommath
> [Digest::SHA256::Native] /usr/bin/ld: cannot find -latomic_ops
> [Digest::SHA256::Native] /usr/bin/ld: cannot find -luv
> [Digest::SHA256::Native] collect2: error: ld returned 1 exit status
> [Digest::SHA256::Native] make: ***
> [/home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so]
> Error 1
> [Digest::SHA256::Native] The spawned command 'make' exited unsuccessfully
> (exit code: 2)
> [Digest::SHA256::Native]   in method build at
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/Build.pm
> line 14
> [Digest::SHA256::Native]   in block  at -e line 1
> ===> Building [FAIL]: Digest::SHA256::Native:ver<0.03>
> [Digest::SHA256::Native] Failed to build, but continuing with --force-build
> ===> Testing: Digest::SHA256::Native:ver<0.03>
> [Digest::SHA256::Native] Cannot locate native library
> '/home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so':
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so:
> cannot open shared object file: No such file or directory
> [Digest::SHA256::Native]   in method setup at
> /usr/lib/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03
> (NativeCall) line 290
> [Digest::SHA256::Native]   in method CALL-ME at
> /usr/lib/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03
> (NativeCall) line 577
> [Digest::SHA256::Native]   in sub sha256-hex at
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/lib/Digest/SHA256/Native.pm6
> (Digest::SHA256::Native) line 19
> [Digest::SHA256::Native]   in sub sha256-hex at
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/lib/Digest/SHA256/Native.pm6
> (Digest::SHA256::Native) line 9
> [Digest::SHA256::Native]   in block  at t/01-basic.t line 6
> [Digest::SHA256::Native] Cannot locate native library
> '/home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so':
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so:
> cannot open shared object file: No such file or directory
> [Digest::SHA256::Native]   in method setup at
> /usr/lib/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03
> (NativeCall) line 290
> [Digest::SHA256::Native]   in method CALL-ME at
> /usr/lib/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03
> (NativeCall) line 577
> [Digest::SHA256::Native]   in sub sha256-hex at
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/lib/Digest/SHA256/Native.pm6
> (Digest::SHA256::Native) line 19
> [Digest::SHA256::Native]   in block  at t/02-blob.t line 7
> [Digest::SHA256::Native] Cannot locate native library
> '/home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so':
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so:
> cannot open shared object file: No such file or directory
> [Digest::SHA256::Native]   in method setup at
> /usr/lib/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03
> (NativeCall) line 290
> [Digest::SHA256::Native]   in method CALL-ME at
> /usr/lib/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03
> (NativeCall) line 577
> [Digest::SHA256::Native]   in sub sha256-hex at
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/lib/Digest/SHA256/Native.pm6
> (Digest::SHA256::Native) line 19
> [Digest::SHA256::Native]   in sub sha256-hex at
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/lib/Digest/SHA256/Native.pm6
> (Digest::SHA256::Native) line 9
> [Digest::SHA256::Native]   in sub sha256 at
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/lib/Digest/SHA256/Native.pm6
> (Digest::SHA256::Native) line 27
> [Digest::SHA256::Native]   in block  at t/03-sha256.t line 6
> [Digest::SHA256::Native] Cannot locate native library
> '/home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so':
> /home/acpguedes/.zef/store/Digest-SHA256-Native-0.03.tar.gz/Digest-SHA256-Native-0.03/resources/libraries/libsha256.so:
> cannot open shared object file: No such file or 

Re: doing an inner join via cross-product

2020-07-20 Thread Aureliano Guedes
With a dataframe data structure and some adjusts and some functions
modifications it could be doing like:

data1 .# perhaps the space after . is intentional to bring the idea of
pipes replace f(x) with x . f() 
 left_join(data2, by={ x = columname_on_data1, y = columname_on_data2}

by calling the column on the join will be performed as a variable.

I found a module that implements the dataframe data structure in Perl5 but
I didn't find it to Raku.
But, I'm thinking in learn Raku better by implementing a DataFrame data
structure (as R and Pandas-Python) and some relational function or grammar
(as R tidyverse/dplyr ) to manipulate
data.

What are the variations of this grep to left, right, and outer joins?

On Sun, Jul 19, 2020 at 5:42 PM Darren Duncan 
wrote:

> This reminds me of my 2009 Set::Relation Perl module, which works to help
> you do
> SQL features like this in your application, but will soon be superseded by
> another module that also has a Raku version. -- Darren Duncan
>
> On 2020-07-19 1:02 p.m., Joseph Brenner wrote:
> > I was thinking about the cross-product operator the other day,
> > and I was wondering if there might be a convenient way of
> > filtering the resulting cartesian product to do something like a
> > database inner join:
> >
> >  my @level  = ( godzilla => 9 ,gremlin => 3, hanuman => 5 );
> >  my @origin = ( godzilla => 'jp',  tingler => 'us',  hanuman => 'il'
> );
> >
> >  my @results = ( @level X @origin ).grep({ $_[0].keys eq $_[1].keys
> });
> >  say @results;  # ((godzilla => 6 godzilla => jp) (hanuman => 5
> > hanuman => il))
> >
> > That's easy enough, though the resulting data structure isn't very neat.
> > I started looking for ways to rearrange it:
> >
> >  my %joined;
> >  for @results -> $row {
> >  say "row: ", $row;  # e.g. row: (godzilla => 9 godzilla
> => jp)
> >  say $row.map({ .keys });# e.g. ((godzilla) (godzilla))
> >  say $row.map({ .values });  # e.g. ((9) (jp))
> >
> >  my $monster =| $row[0].keys;  # e.g. godzilla
> >  my @attributes =| $row.map({ .values });  # e.g. [9 jp]
> >  %joined{ $monster } = @attributes;
> >  }
> >  say %joined;  # {godzilla => [9 jp], hanuman => [5 il]}
> >
> > I can do it more compactly, but it risks getting unreadable:
> >
> >  my %joined2 =| @results.map({ $_[0].keys => .map({ .values }).flat
> });
> >
> > In any case, the %joined structure feels more perlish, for
> > example it's easier to use it to generate reports:
> >
> >  for %joined.keys -> $key {
> >  printf "%12s: level: %-2d origin: %3s\n", $key, %joined{ $key
> }.flat;
> >  }
> >  # hanuman: level: 5  origin:  il
> >  #godzilla: level: 9  origin:  jp
> >
> > Is there some neater way of doing this that I'm missing?
> >
>


-- 
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110


Re: I cannot install any lib with Zef

2020-07-20 Thread JJ Merelo
El lun., 20 jul. 2020 a las 18:03, Aureliano Guedes (<
guedes.aureli...@gmail.com>) escribió:

> Hi all,
>
> I'm trying to figure out why I cannot install any package with zef.
>
> $ raku --version
> This is Rakudo version 2020.06 built on MoarVM version 2020.06
> implementing Raku 6.d.
>
> zef install Acme::Insult::Lala
> ===SORRY!===
> No candidate found for 'zef' that match your criteria.
> Did you perhaps mean one of these?
>   Distribution | Author(ity) | Version
>| |
>| |
>| |
>| |
>

This error does not seem to correspond to the command line... It's as if
you were looking for zef. That happens when you don't have an updated
version of the tool and the repos and nothing downloaded locally.  My hunch
is that you might have an old version of zef; failing that, the repo index
must be corrupted somehow. In any case, install zef (not using zef, but
reinstalling it from repos or with rakudobrew) and you will fix it.

>

-- 
JJ


I cannot install any lib with Zef

2020-07-20 Thread Aureliano Guedes
Hi all,

I'm trying to figure out why I cannot install any package with zef.

$ raku --version
This is Rakudo version 2020.06 built on MoarVM version 2020.06
implementing Raku 6.d.

zef install Acme::Insult::Lala
===SORRY!===
No candidate found for 'zef' that match your criteria.
Did you perhaps mean one of these?
  Distribution | Author(ity) | Version
   | |
   | |
   | |
   | |

I tried to install another package from https://modules.raku.org/ and I get
nothing.


I'm using Ubuntu 18.04.


Any suggestions?

Thanks in advance
-- 
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110


Re: subs and the type system

2020-07-20 Thread Tobias Boege
On Mon, 20 Jul 2020, Gianni Ceccarelli wrote:
> Aside:
> 
> ``(sub (Int $ --> Int) {}) ~~ Walkable`` is false, because
> ``:(Int $ --> Int) ~~ :(Numeric $ --> Numeric)`` is false, which is
> correct because function subtypes should be contravariant in the parameter
> types and covariant in the return type (i.e. if the caller passes a
> Numeric, the callback should accept a Numeric or a *more general*
> type, and if the caller expects a Numeric back, the callback should
> return a Numeric or a *more specific* type).
> 
> But then ``:(Numeric $ --> Int) ~~ :(Numeric $ --> Numeric)`` is also
> false, and I feel like it should be true. Opinions?
> 

My _opinion_ is that you are right, be it only because there exist words
reminiscent of category theory that describe your desired behavior :-)

But the source code of a Signature smartmatching another Signature [1]
explicitly uses smartmatching for the parameter types and uses the
unforgiving container identity operator to compare the return types:

  91  return False unless self.returns =:= $topic.returns;

The `eqv` implementation for two Signatures uses `=:=` as well, so this
restriction seems deliberate. Changing the `=:=` to a `.does` call makes
your code snippet work:

  $ ./rakudo-m -e 'say :(Numeric $ --> Int) ~~ :(Numeric $ --> Numeric)'
  True

but it creates problems with roast that appear to spread multiple parts
of the language. Failing tests are:

  t/spec/S10-packages/precompilation.rakudo.moar  (Wstat: 65280 
Tests: 32 Failed: 0)
  t/spec/S02-types/compact.rakudo.moar(Wstat: 256 
Tests: 0 Failed: 0)
  t/spec/S02-types/native.rakudo.moar (Wstat: 256 
Tests: 0 Failed: 0)
  t/spec/S06-currying/positional.t(Wstat: 256 
Tests: 0 Failed: 0)
  t/spec/S06-signature/closure-parameters.rakudo.moar (Wstat: 256 
Tests: 21 Failed: 1)
  t/spec/S09-typed-arrays/native-int.rakudo.moar  (Wstat: 65280 
Tests: 169 Failed: 0)
  t/spec/S09-typed-arrays/native-num.rakudo.moar  (Wstat: 65280 
Tests: 157 Failed: 0)
  t/spec/S09-typed-arrays/native.t(Wstat: 65280 
Tests: 3 Failed: 0)
  t/spec/S17-lowlevel/cas-int.t   (Wstat: 256 
Tests: 0 Failed: 0)
  t/spec/S17-promise/nonblocking-await.t  (Wstat: 256 
Tests: 0 Failed: 0)
  t/spec/S32-array/splice.rakudo.moar (Wstat: 256 
Tests: 0 Failed: 0)
  t/spec/S32-basics/xxPOS-native.t(Wstat: 65280 
Tests: 15 Failed: 0)
  t/spec/S32-list/iterator.t  (Wstat: 65280 
Tests: 48 Failed: 0)
  t/spec/integration/precompiled.t(Wstat: 256 
Tests: 9 Failed: 1)

Some failures are very dubious like this error, resulting in the test
not even starting (there are multiple of this sort, as you can see):

  $ ./rakudo-m -Ilib t/spec/S06-currying/positional.t
  ===SORRY!===
  No appropriate parametric role variant available for 'array::intarray'

Maybe my small changes (patch attached) create inconsistencies with these
other parts of the language (oftentimes related to native types?), or maybe
the test suite relies on (hence specifies) non-covariance. I don't know.

Best,
Tobias

[1] 
https://github.com/rakudo/rakudo/blob/master/src/core.c/Signature.pm6#L34-L93

PS: I want to note that my first attempt naïvely replaced the `=:=` with
`~~` but this set roast even more on fire which I think is related to `~~`
autothreading on junctions, as opposed to `=:=` and `.does`.

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
diff --git a/src/core.c/Signature.pm6 b/src/core.c/Signature.pm6
index ea2b5cabe..72eff7bdd 100644
--- a/src/core.c/Signature.pm6
+++ b/src/core.c/Signature.pm6
@@ -88,7 +88,7 @@ my class Signature { # declared in BOOTSTRAP
 return False unless $hasslurpy;
 }
 }
-return False unless self.returns =:= $topic.returns;
+return False unless $topic.returns.does(self.returns);
 True;
 }
 
@@ -160,7 +160,7 @@ multi sub infix:(Signature:D \a, Signature:D \b) {
 return False unless a.WHAT =:= b.WHAT;
 
 # different return
-return False unless a.returns =:= b.returns;
+return False unless a.returns.does(b.returns);
 
 # arity or count mismatch
 return False if a.arity != b.arity || a.count != b.count;


Re: subs and the type system

2020-07-20 Thread Gianni Ceccarelli
On Mon, 20 Jul 2020 14:00:33 +0200
Tobias Boege  wrote:
> You cannot write `Walkable ` in the signature of  because the
> combination of a type and the &-sigil apparently means that ``
> should be Callable and return a Walkable. That's why I use the
> $-sigil.

Aha! That's the bit I got wrong, thank you! with that change, my
example works.

I'd still like opinions on the subtyping situation between `Signature`

-- 
Dakkar - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88


Re: subs and the type system

2020-07-20 Thread Theo van den Heuvel

Thanks Gianni and Tobias,

This is very helpful.

One minor follow-up. If I define by signature like this:

my $sgn-walkable = :(Numeric $n --> Numeric);

I could not define Walkable subs like this, could I?

my Walkable $crawlback = ...

Thanks,
Theo

Tobias Boege schreef op 2020-07-20 14:00:

On Mon, 20 Jul 2020, Theo van den Heuvel wrote:

Hi gurus,

after looking at the documentation on Sub, Signature and the raku type
system I find myself unable to constrain the types of functions in the 
way I

think I need.

The situation: I have a function, let's call in 'walker', whose first
parameter is a callback.
I wish to express that only callbacks with a certain Signature and 
return

type are acceptable.
Let's say the callback should follow :(Numeric $n --> Numeric). My 
callback

is going to be more complicated, but for simplicity sake.

I was hoping to be able to use a named type of function with this 
signature.

I know I can say

my $sig =  :(Numeric $n --> Numeric);

but my attempts to use $sig fail. I found no example of an application
beyond smartmatching.

[...]

but I was hoping for an explicit type, maybe using class, or subset or
roles.
I hope to say soemething like

sub walker(Walkable , ...)

What am I missing here?



You said that you know how to check signatures via smartmatching.
Smartmatching is great, there isn't much to know beyond it. The way
to shoehorn smartmatching into the type system is `subset`:

  subset Walkable of Callable where {
  .signature ~~ :(Numeric --> Numeric)
  };

If your callback signature is more complicated, all the more reason to
give it a name via subset. You can use it now almost like you wanted:

  sub walker (Walkable $w) {
  say $w($_) for 1..10
  }

  walker(-> Numeric $x --> Numeric { $x.sqrt });  # WORKS
  walker(-> $x { $x.sqrt });  # DOESN'T

You cannot write `Walkable ` in the signature of  because the
combination of a type and the &-sigil apparently means that `` should
be Callable and return a Walkable. That's why I use the $-sigil. [*]

Note that if you constrain the signature of a Callable, then you have
to provide the appropriate signature statically, as demonstrated.

Best,
Tobias

[*] N.B. I failed to find a reference for this in the documentation.
I discovered it by writing `sub walker (Int )` and provoking a type
checking error which told me that it was expecting `Callable[Int]`.
(That was also my gut feeling.) According to source code, Callable is
a parametric role in Rakudo whose first parameter indeed is the return
type. This is all somewhat nebulous to me (how does the Callable-
parametric way to specify return type compare with smartmatching
against .signature, for example? They don't seem equivalent!), so
I stick with the $-sigil.


Re: subs and the type system

2020-07-20 Thread Tobias Boege
On Mon, 20 Jul 2020, Theo van den Heuvel wrote:
> Hi gurus,
> 
> after looking at the documentation on Sub, Signature and the raku type
> system I find myself unable to constrain the types of functions in the way I
> think I need.
> 
> The situation: I have a function, let's call in 'walker', whose first
> parameter is a callback.
> I wish to express that only callbacks with a certain Signature and return
> type are acceptable.
> Let's say the callback should follow :(Numeric $n --> Numeric). My callback
> is going to be more complicated, but for simplicity sake.
> 
> I was hoping to be able to use a named type of function with this signature.
> I know I can say
> 
> my $sig =  :(Numeric $n --> Numeric);
> 
> but my attempts to use $sig fail. I found no example of an application
> beyond smartmatching.
> 
> [...]
>
> but I was hoping for an explicit type, maybe using class, or subset or
> roles.
> I hope to say soemething like
> 
> sub walker(Walkable , ...)
> 
> What am I missing here?
> 

You said that you know how to check signatures via smartmatching.
Smartmatching is great, there isn't much to know beyond it. The way
to shoehorn smartmatching into the type system is `subset`:

  subset Walkable of Callable where {
  .signature ~~ :(Numeric --> Numeric)
  };

If your callback signature is more complicated, all the more reason to
give it a name via subset. You can use it now almost like you wanted:

  sub walker (Walkable $w) {
  say $w($_) for 1..10
  }

  walker(-> Numeric $x --> Numeric { $x.sqrt });  # WORKS
  walker(-> $x { $x.sqrt });  # DOESN'T

You cannot write `Walkable ` in the signature of  because the
combination of a type and the &-sigil apparently means that `` should
be Callable and return a Walkable. That's why I use the $-sigil. [*]

Note that if you constrain the signature of a Callable, then you have
to provide the appropriate signature statically, as demonstrated.

Best,
Tobias

[*] N.B. I failed to find a reference for this in the documentation.
I discovered it by writing `sub walker (Int )` and provoking a type
checking error which told me that it was expecting `Callable[Int]`.
(That was also my gut feeling.) According to source code, Callable is
a parametric role in Rakudo whose first parameter indeed is the return
type. This is all somewhat nebulous to me (how does the Callable-
parametric way to specify return type compare with smartmatching
against .signature, for example? They don't seem equivalent!), so
I stick with the $-sigil.

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


Re: subs and the type system

2020-07-20 Thread Gianni Ceccarelli
On Mon, 20 Jul 2020 12:37:33 +0200
Theo van den Heuvel  wrote:
> The situation: I have a function, let's call in 'walker', whose first 
> parameter is a callback.
> I wish to express that only callbacks with a certain Signature and 
> return type are acceptable.
> Let's say the callback should follow :(Numeric $n --> Numeric). My 
> callback is going to be more complicated, but for simplicity sake.

Interesting problem!

I tried::

  use v6.d;

  subset Walkable of Callable where { $^c.signature ~~ :(Numeric $--> Numeric) 
};

  sub walker(Walkable ) {
  say callback(1);
  }

  sub callback(Numeric $x --> Numeric) { 1+$x }

  say 'is Callable? ',  ~~ Callable;
  say 'does the signature match? ',  ~~ :(Numeric $n --> 
Numeric);
  say 'is Walkable? ',  ~~ Walkable;

  walker();

which prints::

  is Callable? True
  does the signature match? True
  is Walkable? True
  Type check failed in binding to parameter ''; expected Walkable but 
got Sub+{Callable[Numeric]} (sub callback (Numeric...)
in sub walker at /tmp/s.raku line 5
in block  at /tmp/s.raku line 15

which is quite surprising to me.

Aside:

``(sub (Int $ --> Int) {}) ~~ Walkable`` is false, because
``:(Int $ --> Int) ~~ :(Numeric $ --> Numeric)`` is false, which is
correct because function subtypes should be contravariant in the parameter
types and covariant in the return type (i.e. if the caller passes a
Numeric, the callback should accept a Numeric or a *more general*
type, and if the caller expects a Numeric back, the callback should
return a Numeric or a *more specific* type).

But then ``:(Numeric $ --> Int) ~~ :(Numeric $ --> Numeric)`` is also
false, and I feel like it should be true. Opinions?

-- 
Dakkar - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88


subs and the type system

2020-07-20 Thread Theo van den Heuvel

Hi gurus,

after looking at the documentation on Sub, Signature and the raku type 
system I find myself unable to constrain the types of functions in the 
way I think I need.


The situation: I have a function, let's call in 'walker', whose first 
parameter is a callback.
I wish to express that only callbacks with a certain Signature and 
return type are acceptable.
Let's say the callback should follow :(Numeric $n --> Numeric). My 
callback is going to be more complicated, but for simplicity sake.


I was hoping to be able to use a named type of function with this 
signature.

I know I can say

my $sig =  :(Numeric $n --> Numeric);

but my attempts to use $sig fail. I found no example of an application 
beyond smartmatching.


What I tried so far:
sub walker(:$sig, ...) { ... }

is syntactically incorrect and yields:
  Can only use the : invocant marker in the signature for a method
and only takes a single step in the direction of a more abstract 
solution.


I know I can say
sub walker(:(Numeric $n --> Numeric), ...) { ... }

but I was hoping for an explicit type, maybe using class, or subset or 
roles.

I hope to say soemething like

sub walker(Walkable , ...)

What am I missing here?

Thanks,


--
Theo van den Heuvel
Van den Heuvel HLT Consultancy