unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Function that changes value of a variable
@ 2022-08-21  0:54 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
  0 siblings, 2 replies; 7+ messages in thread
From: wilnerthomas--- via Users list for the GNU Emacs text editor @ 2022-08-21  0:54 UTC (permalink / raw)
  To: Help Gnu Emacs

I am trying to change the value a a variable with the following function, but is not working as intended.

(defun constrain (var min-n max-n)  "Ensure that value of var is between MIN-N and MAX-N inclusive by constraining."  (cond   ((< var min-n)  (setq var min-n))   ((> var max-n)  (setq var max-n))   ((eq var t)     (setq var 1))   ((eq var nil)   (setq var 0))))
Have done the following test, but the value stays 21.

(defvar pingu 21)
(constrain pingu 0 8)
(message "%d" pingu)



^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [External] : Function that changes value of a variable
  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 ` Drew Adams
  2022-08-21  2:40 ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 7+ messages in thread
From: Drew Adams @ 2022-08-21  2:04 UTC (permalink / raw)
  To: wilnerthomas@tutanota.com,
	'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

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

https://emacs.stackexchange.com/q/73192/105

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Function that changes value of a variable
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-08-21  2:40 UTC (permalink / raw)
  To: help-gnu-emacs

> I am trying to change the value a a variable with the following function,
> but is not working as intended.
>
> (defun constrain (var min-n max-n)  "Ensure that value of var is between
> MIN-N and MAX-N inclusive by constraining."  (cond   ((< var min-n)  (setq
> var min-n))   ((> var max-n)  (setq var max-n))   ((eq var t)     (setq var
> 1))   ((eq var nil)   (setq var 0))))

[ Written this way, it's rather illegible.  It was probably manged by your
  MUA because it wasn't labeled as "code" or "preformatted text"
  or somesuch.  ]

> Have done the following test, but the value stays 21.

Of course, because the call only passes the value of the variable, not
the variable itself [ and according your your comment in
https://emacs.stackexchange.com/questions/73192, you know that
already.  ]

Variables aren't first class objects, so you need to use a reference to
the variable:

    (defun my-constrain (varef ...)
      ... (setf (gv-deref varref) ...)
      ...)

You can then pass a reference to your variable with something like:

    (my-constrain (gv-ref pingu) ...)

Of course a simpler solution is to do something else, such as:

    (setq pingu (my-constrain pingu ...))

so `my-constrain` doesn't need to set any variable any more and can be
a pure function instead.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Function that changes value of a variable
  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     ` [External] : " Drew Adams
       [not found]     ` <SJ0PR10MB5488C4F9BF7E4F324B57C6F3F36E9@SJ0PR10MB5488.namprd10.prod.outlook.com-NA-eKDt----2>
  0 siblings, 2 replies; 7+ messages in thread
From: wilnerthomas--- via Users list for the GNU Emacs text editor @ 2022-08-21  3:31 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


Aug 21, 2022, 02:40 by help-gnu-emacs@gnu.org:

>> I am trying to change the value a a variable with the following function,
>> but is not working as intended.
>>
>> (defun constrain (var min-n max-n)  "Ensure that value of var is between
>> MIN-N and MAX-N inclusive by constraining."  (cond   ((< var min-n)  (setq
>> var min-n))   ((> var max-n)  (setq var max-n))   ((eq var t)     (setq var
>> 1))   ((eq var nil)   (setq var 0))))
>>
>
> [ Written this way, it's rather illegible.  It was probably manged by your
>  MUA because it wasn't labeled as "code" or "preformatted text"
>  or somesuch.  ]
>
This time it should show properly

(defvar pingu 21)

(defun constrain (variable min-n max-n)
  "Ensure that value of variable is between MIN-N and MAX-N inclusive by constraining."

  (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))))

(constrain 'pingu 0 8)



>> Have done the following test, but the value stays 21.
>>
>
> Of course, because the call only passes the value of the variable, not
> the variable itself [ and according your your comment in
> https://emacs.stackexchange.com/questions/73192, you know that
> already.  ]
>
> Variables aren't first class objects, so you need to use a reference to
> the variable:
>
>  (defun my-constrain (varef ...)
>  ... (setf (gv-deref varref) ...)
>  ...)
>
> You can then pass a reference to your variable with something like:
>
>  (my-constrain (gv-ref pingu) ...)
>

Not so great with reference


> Of course a simpler solution is to do something else, such as:
>
>  (setq pingu (my-constrain pingu ...))
>
This looks standard, which is good.


> so `my-constrain` doesn't need to set any variable any more and can be
> a pure function instead.
>
>  Stefan
>
Have done 

(defun constrain (variable min-n max-n)
  "Returns value between MIN-N and MAX-N inclusive by constraining."

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

(defvar pingu 21)
(setq pingu (constrain pingu 0 8))

Which gives me 

Lisp error: (wrong-type-argument number-or-marker-p nil)
  <(nil 0)
(cond ((< (symbol-value variable) min-n) min-n) ((> (symbol-value variable) max-n) max-n) ((eq (symbol-value variable) t) 1) ((eq (symbol-value variable) nil) 0))
  constrain(nil 0 8)
  (setq pingu (constrain pingu 0 8))
  (progn (setq pingu (constrain pingu 0 8)))
  eval((progn (setq pingu (constrain pingu 0 8))) t)
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)





^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [External] : Re: Function that changes value of a variable
  2022-08-21  3:31   ` wilnerthomas--- via Users list for the GNU Emacs text editor
@ 2022-08-21 13:47     ` Drew Adams
       [not found]     ` <SJ0PR10MB5488C4F9BF7E4F324B57C6F3F36E9@SJ0PR10MB5488.namprd10.prod.outlook.com-NA-eKDt----2>
  1 sibling, 0 replies; 7+ messages in thread
From: Drew Adams @ 2022-08-21 13:47 UTC (permalink / raw)
  To: wilnerthomas@tutanota.com, Stefan Monnier
  Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

[-- 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 --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [External] : Re: Function that changes value of a variable
       [not found]     ` <SJ0PR10MB5488C4F9BF7E4F324B57C6F3F36E9@SJ0PR10MB5488.namprd10.prod.outlook.com-NA-eKDt----2>
@ 2022-08-21 23:18       ` wilnerthomas--- via Users list for the GNU Emacs text editor
  2022-08-22 14:21         ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: wilnerthomas--- via Users list for the GNU Emacs text editor @ 2022-08-21 23:18 UTC (permalink / raw)
  To: Drew Adams
  Cc: Stefan Monnier, 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'


Aug 21, 2022, 13:47 by drew.adams@oracle.com:

>> (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)))))
>
Function 3   is a very good function.  Thank you so very much for it.
I want to use in a function that requires a numeric value, but should
a user make a mistake by setting `t' on `nil', I can determine his likely
intention.

You do not modify the variable value when the input fall outside the
categories (numeric, t and nil).  Is that because we do not want to wreck
values can could well be other elisp structures (e.g. strings, lists, symbols).
Am I correct?








^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [External] : Re: Function that changes value of a variable
  2022-08-21 23:18       ` wilnerthomas--- via Users list for the GNU Emacs text editor
@ 2022-08-22 14:21         ` Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2022-08-22 14:21 UTC (permalink / raw)
  To: wilnerthomas@tutanota.com
  Cc: Stefan Monnier, 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

> You do not modify the variable value when the input fall outside the
> categories (numeric, t and nil).  Is that because we do not want to wreck
> values can could well be other elisp structures (e.g. strings, lists,
> symbols).
> Am I correct?

I was just trying to guess the behavior _you_ want.
Nothing more than that.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-08-22 14:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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     ` [External] : " Drew Adams
     [not found]     ` <SJ0PR10MB5488C4F9BF7E4F324B57C6F3F36E9@SJ0PR10MB5488.namprd10.prod.outlook.com-NA-eKDt----2>
2022-08-21 23:18       ` wilnerthomas--- via Users list for the GNU Emacs text editor
2022-08-22 14:21         ` Drew Adams

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).