[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Can el-search-query-replace replace mapcar with --map ?
From: |
Michael Heerdegen |
Subject: |
Re: Can el-search-query-replace replace mapcar with --map ? |
Date: |
Fri, 22 Dec 2017 18:17:57 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
Chunyang Xu <mail@xuchunyang.me> writes:
> It gives me:
>
> (--map
> (* x x)
> '(1 2 3))
which is expected, of course.
The transformation you want is not trivial, and there are some
problematic cases when it gets complicated - e.g. when the mapped lambda
uses in its body a local variable with the same name as the argument
variable, like in
#+begin_src emacs-lisp
(lambda (x) (let ((x (1+ x))) (* x x)))
#+end_src
But that should be corner cases in practice.
I would define a helper function to perform the renaming of the lambda
argument into "it" - something like
#+begin_src emacs-lisp
(defun transform-lambda-form-for---map (lambda-form)
(pcase-let ((`(lambda (,var) . ,body) lambda-form))
(macroexpand-1
`(cl-symbol-macrolet ((,var it))
,@body))))
#+end_src
and then el-search-query-replace with the rule
#+begin_src emacs-lisp
`(mapcar ,lambda-form ,list)
->
`(--map ,(transform-lambda-form-for---map lambda-form) ,list)
#+end_src
Does that work ok for you?
Regards,
Michael.