HaloO,

Sam Vilain wrote:
Anyone care to pick holes in this little expression of some Perl 6 core
types as collections?  I mean, other than missing methods ;)

My comments follow.


  role Collection[\$types] {
       has Seq[$types] @.members;
  }

This is a little wrapper that ensures that collections have got
a @.members sequence of arbitrary type. This immediately raises
the question how Seq is defined.


  role Set[::T = Item] does Collection[T] where {
      all(.members) =:= one(.members);
  };

Nice usage of junctions! But how is the assertion of
uniqueness transported into methods that handle adding
of new values? In other words: I doubt that this comes
freely out of the type systems inner workings but is
more a statement of the intentions of a Set. Which leads
to the question when this where clause will be checked
or used to base MMD decisions.


  role Pair[::K = Item, ::V = Item] does Seq[K,V] {
      method key of K { .[0] }
      method value of V { .[1] }
  };
role Mapping[::K = Item, ::V = Item] does Collection[Pair[K,V]] {
      all(.members).does(Pair) and
      all(.members).key =:= one(.members).key;
  }

I guess this is a typo and you wanted a where clause. The first
assertion of the members doing the Pair role should be guaranteed
by using Pair as the type argument when instantiating the Collection
role. Are you sure that the underlying Seq type handles the one and
two parameter forms you've used so far?


  role Hash[Str ::K, ::V = Item] does Mapping[K, V]

Will the Str assertion not be too specific for non-string hashes?

      where { all(.members).key == one(.members).key }
  {
      method post_circumfix:<{ }> (K $key) of V|Undefined {

Nice union type as return type. But isn't the type name Undef?

          my $seq = first { .key == $key } &.members;

Wasn't that @.members?

          $seq ?? $seq.value :: undef;

Ternary is spelled ?? !! now.


      }
  }
role ArrayItem[::V = Item] does Seq[Int, V] {
      method index of Int { .[0] }
      method value of Item { .[1] }
  };

Here we see the two parameter version of Seq at work.


  role Array of Collection[ArrayItem]
      where { all(.members).index == one(.members).index }
  {
      method post_circumfix:<[ ]> (Int $index) of Item {
          my $seq = first { .index == $index } &.members;

Is this first there a grep-like function? Shouldn't it then
read 'first { .index == $index }, @.members'?


          $seq ?? $seq.value :: undef;
      }
  }

I'll take the feedback I get, and try to make a set of Perl 6 classes in
the pugs project that look and feel just like regular Perl 6 hash/arrays
but are expressed in more elementary particles.

This might be very useful in future debates about these types. But
I think everything hinges on the basic Seq type.


Regards, TSa.
--

Reply via email to