newLISP Fan Club

Forum => Anything else we might add? => Topic started by: Jeff on May 24, 2007, 01:20:04 PM

Title: Contexts question
Post by: Jeff on May 24, 2007, 01:20:04 PM
When applying a function defined in a context to a table defined outside of that context, how can you do assoc lookups?  For example:


(context 'foo)
(define (some-fn table)
  (lookup 'key-a table))

(context 'bar)
(some-fn '((key-a "value-a") (key-b "value-b")))


some-fn may be used from various contexts.  In order to lookup key-a, it must know which context we are coming from.  I can do a (name table true), but then to get the correct symbol to look up, I'm forced to do something like:


(eval-string (format "%s:key-a" (string (name table true)))

...which is incredibly verbose syntax just for a table lookup.  Anyone have a solution?
Title:
Post by: Jeff on May 25, 2007, 06:12:17 AM
Never mind.  I've figured it out.



If I create the list in question inside of a context, and then I can pass the context to the function in another context.  With the example above:


(context 'foo)
(define (some-fn ctx)
  (lookup 'ctx:key-a ctx:table))

(context 'bar)
(set 'table ((key-a "value-a") (key-b "value-b")))

(context 'whatever)
(foo:some-fn bar)


Now, foo is reading the context of the input, and can lookup based on the context's symbols in the context's lists.
Title:
Post by: Lutz on May 25, 2007, 08:11:47 AM
... and as an added benefit you have passed your bar:table by reference, which is nice if your are dealing with a lot of data.



Lutz
Title:
Post by: Jeff on May 25, 2007, 09:00:48 AM
I always prefer to do lots of work at the bar:table.  Preferably with a glass:full ;)
Title:
Post by: rickyboy on May 26, 2007, 10:09:10 AM
Quote from: "Jeff"I always prefer to do lots of work at the bar:table.  Preferably with a glass:full ;)

[Click here for audio response.] (//http)  :-)