Get a list of variables currently in scope

Discussion of Common Lisp

Get a list of variables currently in scope

Postby enderw88@gmail.com » 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?
enderw88@gmail.com
 
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Re: Get a list of variables currently in scope

Postby gugamilare » Thu Feb 09, 2012 2:16 pm

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 :)
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Get a list of variables currently in scope

Postby enderw88@gmail.com » 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.
enderw88@gmail.com
 
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Re: Get a list of variables currently in scope

Postby crlf0710 » Thu Feb 09, 2012 9:02 pm

Maybe represent those items with an a-list or something like that will be easier.
crlf0710
 
Posts: 2
Joined: Sat Jan 21, 2012 3:34 am

Re: Get a list of variables currently in scope

Postby nuntius » Thu Feb 09, 2012 10:18 pm

enderw88@gmail.com 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>>>)))))
User avatar
nuntius
 
Posts: 498
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: Get a list of variables currently in scope

Postby gugamilare » Fri Feb 10, 2012 5:28 am

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))))
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Get a list of variables currently in scope

Postby enderw88@gmail.com » 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...
enderw88@gmail.com
 
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm


Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests