From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nick Dokos Subject: Re: lisp: scoping vars in repetitive defuns Date: Tue, 17 Sep 2019 09:42:30 -0400 Message-ID: <874l1bavjt.fsf@alphaville.usersys.redhat.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:46649) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iADko-0004Ga-4C for emacs-orgmode@gnu.org; Tue, 17 Sep 2019 09:42:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iADkl-0008Q8-LX for emacs-orgmode@gnu.org; Tue, 17 Sep 2019 09:42:49 -0400 Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226]:41264 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iADkk-0008PB-30 for emacs-orgmode@gnu.org; Tue, 17 Sep 2019 09:42:47 -0400 Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1iADke-000hbe-R4 for emacs-orgmode@gnu.org; Tue, 17 Sep 2019 15:42:40 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Matt Price writes: > I have a number of convenience functions define to help me with grading assignments. As I go through the semester, i update all of these functions modestly so that they'rehelpful for grading the current > assignment.  > > I big chunk of these simple functions is taken up just declaring variables with (let (())) forms.  Each function uses some ofhte same variables, e.g: > > (defun dh-find-files () >   (interactive) >   (let* ((base (org-entry-get (point) "ORG_LMS_ASSIGNMENT_DIRECTORY")) >          (gh (org-entry-get (point) "GITHUB")) >         (f2o `( ,(concat "Reflection/" gh ".md") ,(concat "students/" gh ".json")))) ;;;; "01/index.html" "02/index.html" "03/style.css" "04/style.css" >     (message "%s" f2o) >     ;; make more flexible for resubmits >     (shell-command (concat "cd " base " && git checkout " gh "-master")) >     (dolist (x f2o) >       (if (file-exists-p (concat base "/" x)) >           (find-file-other-window (concat base "/" x) ) >         (message "File %s does not exist, not opening." x))))) > > (defun dh-tests () >   (interactive) >   (let* ((base (org-entry-get (point) "ORG_LMS_ASSIGNMENT_DIRECTORY" )) >          (gh (org-entry-get (point) "GITHUB"))) >     (with-temp-buffer (shell-command (concat "cd " base " && npm test") t)) ;; the "t" lets us suppress buffer >     (browse-url-of-file (concat base "/TestResults/testresults.html")) >     ;; (dh-mocha-run) >     >     )) > > ---------- > > This semester I changed some elements of my workflow and I had to update all the (org-entry-get) calls to new values.  It makes me think the code is less maintainable than it could be.  I would like to do > something like this: > > (lexical-let ((base `(org-entry-get (point) "ORG_LMS_ASSIGNMENT_DIRECTORY") >                    (gh `(org-entry-get (point) "GITHUB")) ) >     (defun dh-find-files () > (with-temp-buffer (shell-command (concat "cd " base " && npm test") t)) ;; the "t" lets us suppress buffer >     (browse-url-of-file (concat base "/TestResults/testresults.html"))))) > > Obviously it doesn't work this way. But is there any way to set macros like this to be expanded later inside a function definition? I feel certain there must be... Are you overthinking this perhaps? Wouldn't variables work? Like e.g.: (defvar org-lms-assignment-dir-prop "ORG_LMS_ASSIGNMENT_DIRECTORY") (defun dh-find-files() ... (let ((base (org-entry-get (point) org-lms-assignment-dir) ...)))) and then you only have one place to change it? It's quite possible of course that I'm misunderstanding what you are looking for. > > Thanks, > > Matt > -- Nick "There are only two hard problems in computer science: cache invalidation, naming things, and off-by-one errors." -Martin Fowler