Page 1 of 1

Contexts question

Posted: Thu May 24, 2007 8:20 pm
by Jeff
When applying a function defined in a context to a table defined outside of that context, how can you do assoc lookups? For example:

Code: Select all

(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:

Code: Select all

(eval-string (format "%s:key-a" (string (name table true)))
...which is incredibly verbose syntax just for a table lookup. Anyone have a solution?

Posted: Fri May 25, 2007 1:12 pm
by Jeff
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:

Code: Select all

(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.

Posted: Fri May 25, 2007 3:11 pm
by Lutz
... 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

Posted: Fri May 25, 2007 4:00 pm
by Jeff
I always prefer to do lots of work at the bar:table. Preferably with a glass:full ;)

Posted: Sat May 26, 2007 5:09 pm
by rickyboy
Jeff wrote:I always prefer to do lots of work at the bar:table. Preferably with a glass:full ;)
[Click here for audio response.] :-)