emacs-devel
[Top][All Lists]
Advanced

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

Re: Improve `replace-regexp-in-string' ergonomics?


From: Augusto Stoffel
Subject: Re: Improve `replace-regexp-in-string' ergonomics?
Date: Wed, 22 Sep 2021 10:14:56 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

On Wed, 22 Sep 2021 at 06:36, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> We introduced `thread-last' in 2014, and there seems to be one (1) place
> in the Emacs code base, so I guess that didn't take off, but rewriting
> with that, we get:
>
>        (org-babel-read
>         (concat "'"
>               (thread-last
>                 results
>                 (replace-regexp-in-string "'" "\"")
>                 (replace-regexp-in-string ",[[:space:]]" " ")
>                 (replace-regexp-in-string "\\]" ")")
>                   (replace-regexp-in-string "\\[" "("))))
>
> Which is somewhat more readable (but note that this totally breaks down
> if you want to mix in LITERAL etc).

Clojure has a moderately useful named variant of the threading macro for
this kind of situation:

        (as-> expr name & forms)

    Binds name to expr, evaluates the first form in the lexical context
    of that binding, then binds name to that result, repeating for each
    successive form, returning the result of the last form.

(I guess I would have chosen the opposite order of the 'expr' and 'name'
arguments, though.)



reply via email to

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