emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Emacs design and architecture. How about copy-on-write?


From: Emanuel Berg
Subject: Re: Emacs design and architecture. How about copy-on-write?
Date: Tue, 19 Sep 2023 21:34:49 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Ihor Radchenko wrote:

> Because implementation details are tricky - a lot of Elisp
> internal machinery is relying upon modifying global symbol
> objects [...]

Yeah, but that should be fine, as long as they are locked and
unlocked safely, right?

But, if one aspire to reduce the number of global variables
used, a great way of doing that is lexical `let'-closures,
here is an example that allows for the same default value when
the function is called from Lisp and when used as an
interactive command, yet the data is only hardcoded once for
each variable.

With this method - and not just for that use case, also the
use case of 2 functions sharing a variable - one can reduce
global Lisp variable use a lot - I know since I've done that
do my own code, and it works great ever since.

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/math.el

(require 'cl-lib)

;; [...]

(let ((min-def 0)
      (max-def 9)
      (inc-def 1) )
  (cl-defun interval (&optional (min min-def)
                                (max max-def)
                                (inc inc-def) )
    (interactive `(,(read-number "min: " min-def)
                   ,(read-number "max: " max-def)
                   ,(read-number "inc: " inc-def)) )
    (unless (<= min max)
      (error "Bogus interval") )
    (unless (> inc 0)
      (error "Bogus increment") )
    (cl-loop
      for i from min to max by inc
      collect i) )
  (declare-function interval nil) )

;; (interval 10 5)        ; Bogus interval
;; (interval 1 3 -1)      ; Bogus increment
;; (interval 5 10)        ; (5 6 7 8 9 10)
;; (interval 1.8 2.0 0.1) ; (1.8 1.9 2.0)
;; (interval)             ; (0 1 2 3 4 5 6 7 8 9)
;; (interval 19 99)       ; (19 20 21 ... 97 98 99)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]