From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: wilnerthomas--- via Users list for the GNU Emacs text editor Newsgroups: gmane.emacs.help Subject: RE: [External] : Re: Function that changes value of a variable Date: Mon, 22 Aug 2022 01:18:31 +0200 (CEST) Message-ID: References: Reply-To: wilnerthomas@tutanota.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33716"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Stefan Monnier , "'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'" To: Drew Adams Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Aug 22 01:20:26 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oPuF0-0008Yl-IA for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 22 Aug 2022 01:20:26 +0200 Original-Received: from localhost ([::1]:60984 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oPuEz-0004l8-5j for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 21 Aug 2022 19:20:25 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44698) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPuDL-0003Qn-QO for help-gnu-emacs@gnu.org; Sun, 21 Aug 2022 19:18:43 -0400 Original-Received: from w1.tutanota.de ([81.3.6.162]:42426) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPuDK-00005d-4R for help-gnu-emacs@gnu.org; Sun, 21 Aug 2022 19:18:43 -0400 Original-Received: from w3.tutanota.de (unknown [192.168.1.164]) by w1.tutanota.de (Postfix) with ESMTP id 89C98FA0302; Sun, 21 Aug 2022 23:18:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1661123911; s=s1; d=tutanota.com; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Content-Transfer-Encoding:Cc:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender; bh=BPG93QJ7qUrJeuQOXN62TXnszCDeM0MdXPj4U83GfRI=; b=ExI7ChQ/KSjp94mvelMIhQphelrGkqA1RhKd7z0WX68shGzT42i0LbfAd/2UFmXg TxJuBrFkU5XulXSZXUnr2IDwP/9m5a9h2A9COdfI4sv3Y8D/0VJ1LRx0UdxnZYskVOP yrXuGqpfUACxEH2aEd5DrxRSI+X1VQfOq5RgrZDGKuXyXOTtzSQk8tEEDjQ0KsmaNv+ Yg9eIXy95KZ6bS/ujKIuDsj7NIreZhnKav/kFlaKH6P53oLPa6mHEB1A3xnKgBJ64PG hgc+t3A79OYkx4XMw2QjdNespSkEX64U7A54Oxln8U7bYT4IK73i5Rg04duW8pTZLoS eVIp7Nwfqg== In-Reply-To: Received-SPF: pass client-ip=81.3.6.162; envelope-from=wilnerthomas@tutanota.com; helo=w1.tutanota.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:138973 Archived-At: Aug 21, 2022, 13:47 by drew.adams@oracle.com: >> (defun constrain (variable min-n max-n) >> =C2=A0 (cond >> =C2=A0=C2=A0 ((< (symbol-value variable) min-n) >> (set variable min-n)) >> =C2=A0=C2=A0 ((> (symbol-value variable) max-n) >> (set variable max-n)) >> =C2=A0=C2=A0 ((eq (symbol-value variable) t) >> (set variable 1)) >> =C2=A0=C2=A0 ((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)))=20 > (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=C2=A0=C2=A0 is a very good function.=C2=A0 Thank you so very muc= h 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).=C2=A0 Is that because we do not want to wr= eck values can could well be other elisp structures (e.g. strings, lists, symbo= ls). Am I correct?