all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: "wilnerthomas@tutanota.com" <wilnerthomas@tutanota.com>,
	Stefan Monnier <monnier@iro.umontreal.ca>
Cc: "'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'" <help-gnu-emacs@gnu.org>
Subject: RE: [External] : Re: Function that changes value of a variable
Date: Sun, 21 Aug 2022 13:47:47 +0000	[thread overview]
Message-ID: <SJ0PR10MB5488C4F9BF7E4F324B57C6F3F36E9@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)
In-Reply-To: <N9yS2Ty--3-2@tutanota.com>

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

> (defun constrain (variable min-n max-n)
>   (cond
>    ((< (symbol-value variable) min-n)
>     (set variable min-n))
>    ((> (symbol-value variable) max-n)
>     (set variable max-n))
>    ((eq (symbol-value variable) t)
>     (set variable 1))
>    ((eq (symbol-value variable) nil)
>     (set variable 0))))

1. Evaluate the initial value just once - bind
   a local variable.
2. Make obvious to readers that the function
   always sets the var (if numberp, t, or nil)
   - put the `set' on the outside.

(defun constrain (variable min-n max-n)
  (let ((val  (symbol-value variable)))
    (when (or (numberp val) (memq val '(nil t))) 
      (set variable (if (numberp val)
                        (if (< val min-n)
                            min-n
                          (min max-n val))
                      (if val 1 0))))))

3. Your function doesn't change the value, if
   if not a number, t, or nil.  Is that what
   you really want?  If not:

(defun constrain (variable min-n max-n)
  (let ((val  (symbol-value variable)))
    (unless (or (numberp val) (memq val '(nil t)))
      (error "Oops?! `%s' is %S" variable val))
    (set variable (if (numberp val)
                      (if (< val min-n)
                          min-n
                        (min max-n val))
                    (if val 1 0)))))

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 13627 bytes --]

  reply	other threads:[~2022-08-21 13:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-21  0:54 Function that changes value of a variable wilnerthomas--- via Users list for the GNU Emacs text editor
2022-08-21  2:04 ` [External] : " Drew Adams
2022-08-21  2:40 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-08-21  3:31   ` wilnerthomas--- via Users list for the GNU Emacs text editor
2022-08-21 13:47     ` Drew Adams [this message]
     [not found]     ` <SJ0PR10MB5488C4F9BF7E4F324B57C6F3F36E9@SJ0PR10MB5488.namprd10.prod.outlook.com-NA-eKDt----2>
2022-08-21 23:18       ` [External] : " wilnerthomas--- via Users list for the GNU Emacs text editor
2022-08-22 14:21         ` Drew Adams

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

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

  git send-email \
    --in-reply-to=SJ0PR10MB5488C4F9BF7E4F324B57C6F3F36E9@SJ0PR10MB5488.namprd10.prod.outlook.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=wilnerthomas@tutanota.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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.