Contexts question

Started by Jeff, May 24, 2007, 01:20:04 PM

Previous topic - Next topic

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:


(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?
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

Jeff

#1
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.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

Lutz

#2
... 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

Jeff

#3
I always prefer to do lots of work at the bar:table.  Preferably with a glass:full ;)
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

rickyboy

#4
Quote from: "Jeff"I always prefer to do lots of work at the bar:table.  Preferably with a glass:full ;)

http://www.ilovewavs.com/Effects/Music/RimShot.wav">[Click here for audio response.]  :-)
(λx. x x) (λx. x x)