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

  role Collection[\$types] {
       has Seq[$types] @.members;
  }
 
  role Set[::T = Item] does Collection[T] where {
      all(.members) =:= one(.members);
  };
 
  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;
  }
 
  role Hash[Str ::K, ::V = Item] does Mapping[K, V]
      where { all(.members).key == one(.members).key }
  {
      method post_circumfix:<{ }> (K $key) of V|Undefined {
          my $seq = first { .key == $key } &.members;
          $seq ?? $seq.value :: undef;
      }
  }
 
  role ArrayItem[::V = Item] does Seq[Int, V] {
      method index of Int { .[0] }
      method value of Item { .[1] }
  };
 
  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;
          $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.

Cheers,
Sam.

Reply via email to