all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Chunyang Xu <mail@xuchunyang.me>
To: Michael Heerdegen <michael_heerdegen@web.de>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Can el-search-query-replace replace mapcar with --map ?
Date: Sat, 23 Dec 2017 14:17:25 +0800	[thread overview]
Message-ID: <m2k1xe7ytm.fsf@xuchunyang.me> (raw)
In-Reply-To: <87a7yahebe.fsf@web.de>


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	other threads:[~2017-12-23  6:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-22 11:45 Can el-search-query-replace replace mapcar with --map ? Chunyang Xu
2017-12-22 17:17 ` Michael Heerdegen
2017-12-23  6:17   ` Chunyang Xu [this message]
2017-12-23 14:03     ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2k1xe7ytm.fsf@xuchunyang.me \
    --to=mail@xuchunyang.me \
    --cc=help-gnu-emacs@gnu.org \
    --cc=michael_heerdegen@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.