[Top][All Lists]

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

Re: Closures in Emacs and their usage scenarios.

From: Emanuel Berg
Subject: Re: Closures in Emacs and their usage scenarios.
Date: Tue, 28 Sep 2021 04:50:12 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hongyi Zhao wrote:

> I noticed the document on closure here [1] implemented in
> Emacs/Elisp currently. But it's only a very concise and
> short introduction, and I want to know more about the
> closures in Emacs and their usage scenarios. Any helpful
> tips are welcome.
> [1]

As a part of the language, or perhaps how the language is
implemented, I can't say I understand it, but from the
Joe Hacker perspective it is understandable enough, it can be
used so a function (or set of functions) will have a memory
and can use the variables that are part of that memory like
global variables, even tho they aren't and are protected from
outside interference, so that, for example, they wont be reset
between function calls ...

Here is one example - note that the byte-compiler will warn
that "the function ‘align-from-left’ is not known to be
defined." Well, OK.

There is a better/easier example, an example with a counter,
and then three functions within the `let' body, the let holds
the counter value and the functions are "increase", "decrease",
and "reset". Maybe one wants a "print" function as well? OK,
so four functions.

ikr? smells like OO spirit ...

;;; -*- lexical-binding: t -*-
;;; this file:

(require 'cl-lib)

(let ((alf-regexp))
  (defun align-from-left (&optional set-regexp)
    (interactive "p")
    (let ((default-regexp "^\\|[[:punct:]]\\|[[:space:]][[:alnum:]]"))
      (unless (stringp set-regexp)
        (cl-case set-regexp
          ( 4 (setq alf-regexp (read-regexp "regexp: ")))
          (16 (setq alf-regexp default-regexp))
          ( t (unless alf-regexp
                (setq alf-regexp default-regexp) )))))
    (let ((beg (point))
          (re  (or (and (stringp set-regexp) set-regexp)
                    alf-regexp) ))
      (when (re-search-backward re (line-beginning-position) t)
        (while (looking-at "[[:space:]]")
          (forward-char 1) )
        (insert (make-string (- beg (point)) ?\s)) ))))
(defalias 'alf #'align-from-left)

(provide 'align-from-left)

underground experts united

reply via email to

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