help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Closures in Emacs and their usage scenarios.


From: Hongyi Zhao
Subject: Re: Closures in Emacs and their usage scenarios.
Date: Tue, 28 Sep 2021 14:46:28 +0800

On Tue, Sep 28, 2021 at 10:50 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> 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] https://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html
>
> 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 ...

What's the meaning of `OO spirit'?

>
> ;;; -*- lexical-binding: t -*-
> ;;;
> ;;; this file:
> ;;;   https://dataswamp.org/~incal/emacs-init/align-from-left.el
>
> (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
> https://dataswamp.org/~incal
>
>


-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



reply via email to

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