emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: Matt Price <moptop99@gmail.com>
Cc: Org Mode <emacs-orgmode@gnu.org>
Subject: Re: lisp: scoping vars in repetitive defuns
Date: Tue, 17 Sep 2019 08:46:15 -0400	[thread overview]
Message-ID: <CAJ51ETo697vLS2CX8ErUWZ_UZnD0LW5nPp4BiHzy3XVFC+WWow@mail.gmail.com> (raw)
In-Reply-To: <CAN_Dec9uzTtgDrS2vJqprdd=YS=mtzM+-d9yAFK9EsVVvG2h5A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3914 bytes --]

I don't totally understand what you are trying to do here. If this were
Python, it sounds like you want some kind of class that stores a variable
and reuses it several different functions? Something kind of similar to
that in elisp is a closure, which is like what you described. For example,
here, we define a variable a, and then define two functions that use it
persistently.

#+BEGIN_SRC emacs-lisp
(lexical-let ((a 5))
  (defun f1 (x)
    (* a x))

  (defun f2 (x)
    (+ a x)))


(list (f1 2) (f2 2))
#+END_SRC

#+RESULTS:
| 10 | 7 |


I think you can wrap this in a macro to make new functions, e.g.

#+BEGIN_SRC emacs-lisp
(defmacro f-maker (a)
  `(lexical-let ((a ,a))
     (defun f1 (x)
       (* a x))

     (defun f2 (x)
       (+ a x))))

(f-maker 3)

(list (f1 2) (f2 2))
#+END_SRC

#+RESULTS:
| 6 | 5 |


There is also a class system you can probably use like this called eieio,
Here is one approach to doing this.

#+BEGIN_SRC emacs-lisp
(require 'eieio)

(defclass Grade ()
  ((:a :initarg :a)))

(cl-defmethod gf1 ((g Grade) x)
  (* (oref g :a) x))

(cl-defmethod gf2 ((g Grade) x)
  (+ (oref g :a) x))

(let ((G (Grade :a 3)))
  (list (gf1 G 2) (gf2 G 2)))
#+END_SRC

#+RESULTS:
| 6 | 5 |


Finally, maybe the simplest thing to do is pass this information in as an
argument, e.g. a plist or alist, or get them from a function that you only
change in one place?

John

-----------------------------------
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



On Tue, Sep 17, 2019 at 7:31 AM Matt Price <moptop99@gmail.com> wrote:

> 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...
>
> Thanks,
>
> Matt
>

[-- Attachment #2: Type: text/html, Size: 5518 bytes --]

  reply	other threads:[~2019-09-17 12:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-17 11:30 lisp: scoping vars in repetitive defuns Matt Price
2019-09-17 12:46 ` John Kitchin [this message]
2019-09-18  1:12   ` Matt Price
2019-09-18 12:48     ` John Kitchin
2019-09-18 20:52       ` Matt Price
2019-09-18 21:26         ` Joost Kremers
2019-09-18 21:44           ` Adam Porter
2019-09-18 23:17             ` John Kitchin
2019-09-19  0:10               ` Adam Porter
2019-09-18 21:42         ` Adam Porter
2019-09-17 13:42 ` Nick Dokos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJ51ETo697vLS2CX8ErUWZ_UZnD0LW5nPp4BiHzy3XVFC+WWow@mail.gmail.com \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=moptop99@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).