unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Okam via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 47261@debbugs.gnu.org
Subject: bug#47261: Fwd: bug#47261: Destructuring with Pcase without assigning values
Date: Mon, 19 Apr 2021 22:10:28 +0000	[thread overview]
Message-ID: <d52d950c-d003-6b00-d4ed-198afcec06a5@protonmail.com> (raw)
In-Reply-To: <4925d1a5-4669-8290-e534-1662f13ca183@protonmail.com>

For the record, I am forwarding this reply to the bug tracker. I forgot
to send the original message to the tracker.


-------- Forwarded Message --------
Subject: Re: bug#47261: Destructuring with Pcase without assigning values
Date: Sun, 18 Apr 2021 15:15:00 -0400
From: Okam <okamsn@protonmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>

On 4/17/21 7:37 PM, Stefan Monnier wrote:
>
>> In the `cl-loop`-like macro that I am writing, things are currently set
>> up so that variables being assigned values in accumulation clauses can
>> be automatically used as the return value of the macro.
>>
>>       ;; => ((1 3 5) (2 4 6))
>>       (loopy (flag pcase)
>>              (list i '((1 2) (3 4) (5 6)))
>>              (collect `(,a ,b) i))
>>
>> This behavior requires finding the variables in the correct order.
>>
>> The getting of the variables used ("a" and "b" above) can be done when
>> using the new `pcase-compile-pattern`, if the variables are fed to the
>> CODEGEN argument in the order that they are written, or I can try to
>> find them manually, if possible.
>
> I still don't understand.  AFAICT what the (collect `(,a ,b) i) above is
> expected to do is something more or less equivalent to:
>
>      (push (car i) a)
>      (push (cadr i) b)
>
> right?
>
> So a CODEGEN which does
>
>     (lambda (varvals _count &rest _)
>       (mapcar (lambda (varval)
>                 `(push ,(cadr varval) ,(car varval)))
>               varvals))
>
> should do the trick, regardless of the order in which the vars
> are passed.
>
> I think I'm still missing something in your explanation.
>
>
>          Stefan
>

What I mean to say is that, if possible, I would like to add the
variables in the pattern to a list of values to return.  This requires
identifying the symbols which represent the variables.

Using CODEGEN works for setting the values, yes, and is not sensitive to
the order in which it receives variables. However, I am currently using
the CODEGEN step to add the variables to a list of returned values,
since CODEGEN is already being given the symbol for each variable.

I am trying to have it so that these variables are listed in the return
value in the same order that they are listed in the pattern.

For example, this

      ;; => (4 6)
      (loopy (flag pcase)
             (list i '([1 2] [3 4]))
             (sum `[,sum1 ,sum2] i))

currently expands to

      (let ((sum2 0)
            (sum1 0))
        (let* ((list-108 '([1 2] [3 4]))
               (i nil))
          (cl-block nil
            (while (consp list-108)
              (setq i (car list-108))
              (if (vectorp i)
                  (let* ((x109 (length i)))
                    (if (eql x109 2)
                        (let* ((x110 (pcase--flip aref 0 i))
                               (x111 (pcase--flip aref 1 i)))
                          (progn
                            (setq sum2 (+ x111 sum2))
                            (setq sum1 (+ x110 sum1)))))))
              (setq list-108 (cdr list-108)))
            (list sum1 sum2))))

where `sum1` and `sum2` are in the same order that they appear in the
pattern.  This works with what I have tested, but I am unsure of whether
it is dependable.

So, I am asking whether the variables passed to CODEGEN can be relied on
to be in a particular order for destructuring patterns, or whether there
is a good way to find the variables and their order manually.

For example, for `seq-let`-like destructuring, my experience is that the
variables are given in the reverse order and that variables can be
identified as symbols that are not `&rest`.






  parent reply	other threads:[~2021-04-19 22:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-19 13:47 bug#47261: Destructuring with Pcase without assigning values Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-03-19 21:45 ` Stefan Monnier
2021-04-17  1:24   ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-04-17  3:22     ` Stefan Monnier
     [not found]       ` <1dacded9-3e40-bc02-56c7-a157d739ffb7@protonmail.com>
2021-04-19 22:06         ` bug#47261: Fwd: " Earl Hyatt via Bug reports for GNU Emacs, the Swiss army knife of text editors
     [not found]         ` <jwveef84hcl.fsf-monnier+emacs@gnu.org>
     [not found]           ` <4925d1a5-4669-8290-e534-1662f13ca183@protonmail.com>
2021-04-19 22:10             ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2022-06-24 18:36   ` Lars Ingebrigtsen

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=d52d950c-d003-6b00-d4ed-198afcec06a5@protonmail.com \
    --to=bug-gnu-emacs@gnu.org \
    --cc=47261@debbugs.gnu.org \
    --cc=okamsn@protonmail.com \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).