Context List

Q&A's, tips, howto's
Locked
Grundle
Posts: 15
Joined: Mon Mar 28, 2005 9:26 pm

Context List

Post by Grundle »

Is it possible to make a list of contexts? For example the following is a snippet of code (that obviously doesn't work) that i have written.

Code: Select all


(context 'ITEM)

	(set 'title "")
	(set 'link "")
	(set 'content "")
	(set 'item_date "")
	(set 'author "")

(context 'MY_CONTEXT)

(set 'items '())

(define (create_item this_item)
	(new ITEM 'MAIN:item-obj)
	;;Item Title
	(set 'item_obj:title "some title")
	;;Item Author
	(set 'item_obj:author "some author")
	;;Item Date
	(set 'item_obj:item_date "some date)
	;;Item Body
	(set 'item_obj:content "foo body")
	;;Item Link/URL
	(set 'item_obj:link "some URL")
	(cons items item_obj)
)

;;Have some method here that calls create_items multiple times

That is a scaled down version (for brevity's sake). Hopefully this gives a general idea of what I am trying to do. Can anyone advise me on the best approach?

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

Post by Lutz »

This will give you a list of contexts:

Code: Select all

(filter context? (map eval (symbols 'MAIN)))
In your code you probably mean:

Code: Select all

(context 'MY_CONTEXT)

(set 'items '())

(define (create_item this_item)
   (new ITEM this_item)
   (set 'this_item (eval this_item))
   ;;Item Title
   (set 'this_item:title "some title")
   ;;Item Author
   (set 'this_item:author "some author")
   ;;Item Date
   (set 'this_item:item_date "some date")
   ;;Item Body
   (set 'this_item:content "foo body")
   ;;Item Link/URL
   (set 'this_item:link "some URL")
   (push this_tem items)
) 
(context 'MAIN)

;; now you could do: 

(MY_CONTEXT:create_item 'TEST-1)
(MY_CONTEXT:create_item 'TEST-2)

MY_CONTEXT:items => (TEST-2 TEST-1)

; from inside MY_CONTEXT you would do:

(create_item 'MAIN:TEST-1)
(create_item 'MAIN:TEST-1)

; because context symbols always belong to MAIN
This is the critical statement:

(set 'this_item (eval this_item))

Because you want to acces the context which is inside this_item but 'this_item' contains the symbol 'TEST-1 (which will contan the context TEST-1). So with 'eval' you have to peel off one layer.

In the last statment I use 'push', because 'cons' is non-destructive and would not change the list.

Lutz

Grundle
Posts: 15
Joined: Mon Mar 28, 2005 9:26 pm

Post by Grundle »

Thanks for the pointers, I found your explanation extremely helpful. It turns out that the only thing I really needed to change was the following

Code: Select all

(cons items item_obj) 
To this

Code: Select all

(push (eval item_obj) items)
I wasn't very specific about what "this_item" was and I noticed that you thought it was a context being passed, but it actually was a list of lists being passed. Even so, your explanation of contexts and how they interact with each other answered a lot of questions for me.

Thanks!

Locked