[Prev][Next][Index][Thread]

Re: Multiply-keyed collection classes



On Fri, 14 Apr 2000, Scott McKay wrote:
> If I were doing it, I would implement the dfa as a subclass of
> <table> and define a 'table-protocol' that combines both of the keys.
> The function 'sequence-hash' from the 'table-extensions' library
> might be a reasonable place to start.

That sounds reasonable for the state transition table scenario suggested,
but it doesn't help with making the syntax "pretty".  Neel, I suspect the
Dylan-style thing here would be option 2 (lookup(dfa, old-state, char)) --
you may be stuck thinking "pretty = C++/Java" ;-)  The other thing is I'm
not sure whether or not your dfa really "feels like" a <collection>.

OTOH, I don't have my DRM to hand but I think that the description of the
"arr[a, b, ..., n]" macro may say that it translates to "aref(arr, a, b, 
..., n)" with aref looked up _in the current lexical scope_, rather than
in the Dylan module/library.  (IIRC, this is why calling variables bound
to collection elements "element" is a bad thing :-)  That means you could
write your own aref "wrapper" GF, like this:

  // In a module which renames the original aref to dylan/aref.
  // (I'm guessing the signature of aref from memory.)
  define open generic aref (array, #rest indices) => value;

  define sealed method aref (array :: <array>, #rest indices) => value
    apply(dylan/aref, array, indices)
  end;

  define method aref (dfa :: <dfa>, #rest indices) => value
    // etc
  end;


> Neel Krishnaswami wrote in message ...
> >...
> >What's the right way of building of building collection classes whose
> >keys are logically tuples of non-integer objects? Ideally, I'd like to
> >be able to write code like this (for a hypothetical regexp engine):
> >
> >  ...
> >  let new-state = dfa[old-state, character];
> >  ...
> >
> >which looks like a job for the aref generic. However, ... "...[<array>
> >is the] class of collections whose elements are arranged according to
> >a Cartesian coordinate system. [...] ...
> >
> >Right now, I see two possible solutions ...
> >
> >1. I could just ignore this by defining an <array> subclass with an
> >   aref method that takes <object> indices ...

I agree with you that that's an abuse: subclasses of <sequence> should
only have non-negative integer keys.

> >2. I could just write a different method that reads like:
> >
> >     let new-state = lookup(dfa, old-state, character);

As I said, this is probably a good enough answer unless you really are
storing your dfa as a table.

> >3. I could add a slot to the dfa class ...
> >
> >     let new-state = dfa.find(old-state, character);
> >
> > But this is just twisted, and hence doesn't count. :) 

This may be useful sometimes, as (assuming the slot is instance allocated,
rather than class or each-subclass) it allows per-object "methods".  But
it's a hack here :-)




Follow-Ups: References: