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

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

Re: Can el-search-query-replace replace mapcar with --map ?


From: Chunyang Xu
Subject: Re: Can el-search-query-replace replace mapcar with --map ?
Date: Sat, 23 Dec 2017 14:17:25 +0800
User-agent: mu4e 0.9.18; emacs 27.0.50

Michael Heerdegen writes:

> 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?

It works fine for my example:

  (mapcar (lambda (x) (* x x)) '(1 2 3))

but not

  (mapcar (lambda (x) (let ((x (1+ x))) (* x x))) '(1 2 3))

with the exact same patterns, 'el-search-query-replace' reports

  el-search--format-replacement: Error in ‘el-search--format-replacement’ - 
please make a bug report

I guess this is excepted by you, even though
'transform-lambda-form-for---map' is correct for both cases:

  (transform-lambda-form-for---map
   (lambda (x) (* x x)))
       => (* it it)

  (transform-lambda-form-for---map
   (lambda (x) (let ((x (1+ x))) (* x x))))
       => (let ((x (1+ it))) (* x x))


By the way, is there a way to tidy the code after
'el-search-query-replace' ? For the same example above, it produces

  (--map
   (* it it)
   '(1 2 3))

but I want

  (--map (* it it) '(1 2 3))

right now I have to do it manually via M-^ ('delete-indentation'), which
is not very convenient.


Thanks for your help.



reply via email to

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