Page 1 of 1

Get a list of variables currently in scope

Posted: Thu Feb 09, 2012 8:42 am
Is there a way to get a list of all of the symbols currently in scope?

For example

Code: Select all

(defun test-conditions (state)
    (let ((test-1 (list "Test 1 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>)))
           (test-2 (list "Test 2 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>)))
           (test-3 (list "Test 3 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>)))
           (test-4 (list "Test 4 Name" 
                                  (lambda (state) <<<run tests on state return a value>>>))))
      (PROCESS RESULTS (GET-ALL-TEST-RESULTS))))
GET-ALL-TEST-RESULTS would return (test-1 test-2 test-3 test-4)

That way if I want to add a test I just add the clause in the let. Or is there a completely different way of doing this that I haven't thought of?

Re: Get a list of variables currently in scope

Posted: Thu Feb 09, 2012 2:16 pm
by gugamilare
There is no portable way of doing that. The only way to do that would be using a macro with an &env argument and inspecting it to know what symbols are in scope. However, "environments" differ from implementation to implementation. Of course, you can try to do that using tools provided by your particular implementation :)

Re: Get a list of variables currently in scope

Posted: Thu Feb 09, 2012 2:58 pm
Thanks. At least I am asking hard questions now! There must be a cleaner way to implement this. I will keep thinking.

Re: Get a list of variables currently in scope

Posted: Thu Feb 09, 2012 9:02 pm
by crlf0710
Maybe represent those items with an a-list or something like that will be easier.

Re: Get a list of variables currently in scope

Posted: Thu Feb 09, 2012 10:18 pm
by nuntius
[email protected] wrote:Or is there a completely different way of doing this that I haven't thought of?
Something like the following?

Code: Select all

(defun test-conditions (state)
  (process results
   (list
    (list "Test 1 Name" 
          (lambda (state) <<<run tests on state return a value>>>))
    (list "Test 2 Name" 
          (lambda (state) <<<run tests on state return a value>>>))
    (list "Test 3 Name" 
          (lambda (state) <<<run tests on state return a value>>>))
    (list "Test 4 Name" 
          (lambda (state) <<<run tests on state return a value>>>)))))

Re: Get a list of variables currently in scope

Posted: Fri Feb 10, 2012 5:28 am
by gugamilare
Maybe a macro:

Code: Select all

(defmacro with-tests (vars &body body)
  `(let ,vars
     (flet ((get-all-test-results ()
              (list ,@(mapcar #'first vars))))
       ,@body)))

(defun test-conditions (state)
  (with-tests ((test-1 (list "Test 1 Name"
                             (lambda (state) <<<run tests on state return a value>>>)))
               (test-2 (list "Test 2 Name"
                             (lambda (state) <<<run tests on state return a value>>>)))
               (test-3 (list "Test 3 Name"
                             (lambda (state) <<<run tests on state return a value>>>)))
               (test-4 (list "Test 4 Name"
                             (lambda (state) <<<run tests on state return a value>>>))))
    (process results (get-all-test-results))))

Re: Get a list of variables currently in scope

Posted: Fri Feb 10, 2012 2:00 pm
nuntius wrote:

Code: Select all

  (defun test-conditions (state)
      (process results
       (list
        (list "Test 1 Name"
              (lambda (state) <<<run tests on state return a value>>>))
        (list "Test 2 Name"
              (lambda (state) <<<run tests on state return a value>>>))
        (list "Test 3 Name"
              (lambda (state) <<<run tests on state return a value>>>))
        (list "Test 4 Name"
              (lambda (state) <<<run tests on state return a value>>>)))))
n tests on state return a value>>>))))
    (process results (get-all-test-results))))

I came up with a solution very similar to this, but move the common tests into a labels expression...