Contexts question

For the Compleat Fan
Locked
Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Contexts question

Post 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?
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post 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.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

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

Artful code

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post 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.] :-)
(λx. x x) (λx. x x)

Locked