Re: Associative collection with automatic keys?

2017-04-05 Thread Luca Ferrari
On Tue, Apr 4, 2017 at 11:31 AM, Trey Ethan Harris  wrote:
> (I can use classify-list to do this ad-hoc, or even just do a simple map as
> I might in Perl 5, but I'm thinking of each key's relationship to and
> extraction from its value as being a feature of the collection, not of the
> way I construct it.)

Uhm...not clear to me the aim, but sounds like you want to index
differently objects depending on the type of the collection you are
using.
If this is the case, and the adoption of the suggested .WHICH is
therefore not appropriate, you must have a way to map-and-sort your
objects using different attributes. I don't see a very elegant way to
achieve this without having to build an ad-hoc container...
Otherwise, the .WHICH is what you are looking for.

Luca


Re: Associative collection with automatic keys?

2017-04-04 Thread Elizabeth Mattijsen
> On 4 Apr 2017, at 11:31, Trey Ethan Harris  wrote:
> 
> I'm thinking of a Hash-like collection where I can add objects using a 
> index-less append operation, but then have random access to the elements 
> based on a key provided by the object. (For instance, imagine a ProcessTable, 
> where I create the collection telling it to index by the .pid method, add 
> each process to %table in serial without mentioning pid, and then look up the 
> one referring to the interpreter with %table{$*PID}.)

This is basically the approach that object hashes take internally.  So, feels 
to me you just need object hashes?

   https://docs.perl6.org/type/Hash#index-entry-object_hash

Objects are internally keyed to the result of the .WHICH method on that object. 
 If you create a class, one will be provided for you automatically.  But 
there’s nothing preventing you from making your own .WHICH method, as long as 
the result is consistent for a given object and the same for all objects you 
think should be considered identical, you should be fine.


> I really thought this was in Perl 6, but I can't find it in the docs, so 
> maybe I'm thinking of another language. (I think both Ruby and Python have 
> adverbial-like options to do things like this with dictionaries, so maybe 
> that's what I'm thinking of.) Or maybe it's a simple tweak to an existing 
> collection, or just something I need to implement in my element objects

If this is not what you’re looking for, perhaps you can elaborate a bit more, 
with some code examples?



Liz