unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Quick pcase question
@ 2016-03-02 10:12 Eric Abrahamsen
  2016-03-02 11:02 ` Michael Heerdegen
  2016-03-02 12:40 ` Phillip Lord
  0 siblings, 2 replies; 13+ messages in thread
From: Eric Abrahamsen @ 2016-03-02 10:12 UTC (permalink / raw)
  To: help-gnu-emacs

I'm enjoying pcase quite a bit, particular when employed as a kind of
improved destructuring-bind. I think I've got the hang of most of it
(despite the docs being a bit... succinct), but have one leftover
question I keep running into.

What I want to do is: "if this atom matches a predicate, the whole
pattern matches, and also bind the atom to this local variable."

Right now I'm making that work like this:

(setq tst '(symbolla . "I'm the string"))

(pcase tst
  (`(symbolla . ,(and x (pred stringp)))
   (message "cdr is a string: %s" x)))

Is the ",(and x (pred stringp))" part really the simplest way of doing
that? It just seems a little unintuitive.

Thanks,
Eric




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 10:12 Quick pcase question Eric Abrahamsen
@ 2016-03-02 11:02 ` Michael Heerdegen
  2016-03-02 11:18   ` Eric Abrahamsen
  2016-03-02 12:40 ` Phillip Lord
  1 sibling, 1 reply; 13+ messages in thread
From: Michael Heerdegen @ 2016-03-02 11:02 UTC (permalink / raw)
  To: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> (pcase tst
>   (`(symbolla . ,(and x (pred stringp)))
>    (message "cdr is a string: %s" x)))
>
> Is the ",(and x (pred stringp))" part really the simplest way of doing
> that?

Yes, I think so.

How could it be much simpler (there is not much redundancy in that
expression)?


Michael




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 11:02 ` Michael Heerdegen
@ 2016-03-02 11:18   ` Eric Abrahamsen
  2016-03-02 11:38     ` Michael Heerdegen
  2016-03-02 17:18     ` Phillip Lord
  0 siblings, 2 replies; 13+ messages in thread
From: Eric Abrahamsen @ 2016-03-02 11:18 UTC (permalink / raw)
  To: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> (pcase tst
>>   (`(symbolla . ,(and x (pred stringp)))
>>    (message "cdr is a string: %s" x)))
>>
>> Is the ",(and x (pred stringp))" part really the simplest way of doing
>> that?
>
> Yes, I think so.
>
> How could it be much simpler (there is not much redundancy in that
> expression)?

It's not so much the simplicity, I guess, as the intuitiveness -- "(and x"
looks like a test to me, not an assignment. My brain would prefer:

(,x (pred stringp))

But there's no reason to listen to my brain :)

Thanks,
Eric




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 11:18   ` Eric Abrahamsen
@ 2016-03-02 11:38     ` Michael Heerdegen
  2016-03-02 11:56       ` Eric Abrahamsen
  2016-03-02 17:18     ` Phillip Lord
  1 sibling, 1 reply; 13+ messages in thread
From: Michael Heerdegen @ 2016-03-02 11:38 UTC (permalink / raw)
  To: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> It's not so much the simplicity, I guess, as the intuitiveness --
> "(and x" looks like a test to me, not an assignment.

That's a valid point, I think.

Maybe it would be better to avoid reusing names of frequently used
functions.  We could e.g. use "all" or "every" instead of "and", "any"
or "some" instead of "or", and "bind" instead of "let", to avoid
misleading associations in our brains.


Regards,

Michael.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 11:38     ` Michael Heerdegen
@ 2016-03-02 11:56       ` Eric Abrahamsen
  2016-03-02 12:27         ` Michael Heerdegen
  0 siblings, 1 reply; 13+ messages in thread
From: Eric Abrahamsen @ 2016-03-02 11:56 UTC (permalink / raw)
  To: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> It's not so much the simplicity, I guess, as the intuitiveness --
>> "(and x" looks like a test to me, not an assignment.
>
> That's a valid point, I think.
>
> Maybe it would be better to avoid reusing names of frequently used
> functions.  We could e.g. use "all" or "every" instead of "and", "any"
> or "some" instead of "or", and "bind" instead of "let", to avoid
> misleading associations in our brains.

I like the idea of variable assignment being a syntax thing -- it should
be obvious from the position of the symbol within the pcase test that
it's an assignment. Not that I'm really expecting the pcase syntax to
change because of my complaint!

But the fact is that pcase is (very complex and very useful) syntax
sugar. I just wrote a pcase clause that looks like:

`((,(and f-string (pred stringp)) . ,(and class (pred symbolp))) . ,(and regexp (pred stringp)))

And that's kind of awful. The whole point of pcase should be concision,
but that looks pretty terrible.

Anyway, I'm just trying to get used to all the new toys...

E




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 11:56       ` Eric Abrahamsen
@ 2016-03-02 12:27         ` Michael Heerdegen
  2016-03-02 17:25           ` Phillip Lord
  0 siblings, 1 reply; 13+ messages in thread
From: Michael Heerdegen @ 2016-03-02 12:27 UTC (permalink / raw)
  To: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> But the fact is that pcase is (very complex and very useful) syntax
> sugar. I just wrote a pcase clause that looks like:
>
> `((,(and f-string (pred stringp)) . ,(and class (pred symbolp)))
> . ,(and regexp (pred stringp)))
>
> And that's kind of awful. The whole point of pcase should be concision,
> but that looks pretty terrible.

:-P

Maybe separate the testing and the binding operations, like:


#+begin_src emacs-lisp

  (and `((,(pred stringp) . ,(pred symbolp)) . ,(pred stringp))
       `((,f-string       . ,class         ) . ,regexp))

#+end_src


Regards,

Michael.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 10:12 Quick pcase question Eric Abrahamsen
  2016-03-02 11:02 ` Michael Heerdegen
@ 2016-03-02 12:40 ` Phillip Lord
  1 sibling, 0 replies; 13+ messages in thread
From: Phillip Lord @ 2016-03-02 12:40 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> I'm enjoying pcase quite a bit, particular when employed as a kind of
> improved destructuring-bind. I think I've got the hang of most of it
> (despite the docs being a bit... succinct)

They are being improved! It might be worth looking at the docs in
the Emacs-25 pre-releases.


> What I want to do is: "if this atom matches a predicate, the whole
> pattern matches, and also bind the atom to this local variable."
>
> Right now I'm making that work like this:
>
> (setq tst '(symbolla . "I'm the string"))
>
> (pcase tst
>   (`(symbolla . ,(and x (pred stringp)))
>    (message "cdr is a string: %s" x)))
>
> Is the ",(and x (pred stringp))" part really the simplest way of doing
> that? It just seems a little unintuitive.


I think that's it I am afraid. Don't trust me though, I am no expert.

Phil



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 11:18   ` Eric Abrahamsen
  2016-03-02 11:38     ` Michael Heerdegen
@ 2016-03-02 17:18     ` Phillip Lord
  2016-03-03  1:08       ` Eric Abrahamsen
  1 sibling, 1 reply; 13+ messages in thread
From: Phillip Lord @ 2016-03-02 17:18 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Michael Heerdegen <michael_heerdegen@web.de> writes:
>
>> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>>
>>> (pcase tst
>>>   (`(symbolla . ,(and x (pred stringp)))
>>>    (message "cdr is a string: %s" x)))
>>>
>>> Is the ",(and x (pred stringp))" part really the simplest way of doing
>>> that?
>>
>> Yes, I think so.
>>
>> How could it be much simpler (there is not much redundancy in that
>> expression)?
>
> It's not so much the simplicity, I guess, as the intuitiveness -- "(and x"
> looks like a test to me, not an assignment. My brain would prefer:
>
> (,x (pred stringp))

What about?

(guard (stringp ,x))

Phil



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 12:27         ` Michael Heerdegen
@ 2016-03-02 17:25           ` Phillip Lord
  2016-03-03  1:13             ` Eric Abrahamsen
  2016-03-03  3:47             ` Stefan Monnier
  0 siblings, 2 replies; 13+ messages in thread
From: Phillip Lord @ 2016-03-02 17:25 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> But the fact is that pcase is (very complex and very useful) syntax
>> sugar. I just wrote a pcase clause that looks like:
>>
>> `((,(and f-string (pred stringp)) . ,(and class (pred symbolp)))
>> . ,(and regexp (pred stringp)))
>>
>> And that's kind of awful. The whole point of pcase should be concision,
>> but that looks pretty terrible.
>
> :-P
>
> Maybe separate the testing and the binding operations, like:

What about this?

(pcase tst
  ((and
    `(symbolla . ,x)
    (guard (stringp x)))
   (message "cdr is a string: %s" x)))

It separates out the list destructure and the guard. It seems cleaner to
be because only the list appears to be backticked and then unquoted.


A nice addition to pcase would perhaps be automatic "and". So having a
list like so

(pcase tst
  (`(symbolla . ,x)
   (guard (stringp x)))
  (message "cdr is a string: %s" x))

would automatically be interpreted as "and". Not sure if this works
syntactically, though.

Phil



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 17:18     ` Phillip Lord
@ 2016-03-03  1:08       ` Eric Abrahamsen
  2016-03-03 12:08         ` Phillip Lord
  0 siblings, 1 reply; 13+ messages in thread
From: Eric Abrahamsen @ 2016-03-03  1:08 UTC (permalink / raw)
  To: help-gnu-emacs

phillip.lord@russet.org.uk (Phillip Lord) writes:

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Michael Heerdegen <michael_heerdegen@web.de> writes:
>>
>>> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>>>
>>>> (pcase tst
>>>>   (`(symbolla . ,(and x (pred stringp)))
>>>>    (message "cdr is a string: %s" x)))
>>>>
>>>> Is the ",(and x (pred stringp))" part really the simplest way of doing
>>>> that?
>>>
>>> Yes, I think so.
>>>
>>> How could it be much simpler (there is not much redundancy in that
>>> expression)?
>>
>> It's not so much the simplicity, I guess, as the intuitiveness -- "(and x"
>> looks like a test to me, not an assignment. My brain would prefer:
>>
>> (,x (pred stringp))
>
> What about?
>
> (guard (stringp ,x))

You're suggesting that as a possible alternate syntax, right? (In which
case I like it.) It doesn't actually work now, does it?




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 17:25           ` Phillip Lord
@ 2016-03-03  1:13             ` Eric Abrahamsen
  2016-03-03  3:47             ` Stefan Monnier
  1 sibling, 0 replies; 13+ messages in thread
From: Eric Abrahamsen @ 2016-03-03  1:13 UTC (permalink / raw)
  To: help-gnu-emacs

phillip.lord@russet.org.uk (Phillip Lord) writes:

> Michael Heerdegen <michael_heerdegen@web.de> writes:
>
>> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>>
>>> But the fact is that pcase is (very complex and very useful) syntax
>>> sugar. I just wrote a pcase clause that looks like:
>>>
>>> `((,(and f-string (pred stringp)) . ,(and class (pred symbolp)))
>>> . ,(and regexp (pred stringp)))
>>>
>>> And that's kind of awful. The whole point of pcase should be concision,
>>> but that looks pretty terrible.
>>
>> :-P
>>
>> Maybe separate the testing and the binding operations, like:
>
> What about this?
>
> (pcase tst
>   ((and
>     `(symbolla . ,x)
>     (guard (stringp x)))
>    (message "cdr is a string: %s" x)))

That seems quite a bit better -- I really haven't yet wrapped my head
around the possible permutations of upatterns and qpatterns. Still a bit
ungainly when you have multiple clauses, but it does look a little more
intuitive.

Thanks to you both!

E




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-02 17:25           ` Phillip Lord
  2016-03-03  1:13             ` Eric Abrahamsen
@ 2016-03-03  3:47             ` Stefan Monnier
  1 sibling, 0 replies; 13+ messages in thread
From: Stefan Monnier @ 2016-03-03  3:47 UTC (permalink / raw)
  To: help-gnu-emacs

>   (`(symbolla . ,x)
>    (guard (stringp x)))
>   (message "cdr is a string: %s" x))

A more promising direction is to change the syntax of `guard` from
(guard EXP) to (guard PAT EXP), which would be equivalent to (and PAT
(guard EXP)).


        Stefan




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Quick pcase question
  2016-03-03  1:08       ` Eric Abrahamsen
@ 2016-03-03 12:08         ` Phillip Lord
  0 siblings, 0 replies; 13+ messages in thread
From: Phillip Lord @ 2016-03-03 12:08 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> phillip.lord@russet.org.uk (Phillip Lord) writes:
>>> It's not so much the simplicity, I guess, as the intuitiveness -- "(and x"
>>> looks like a test to me, not an assignment. My brain would prefer:
>>>
>>> (,x (pred stringp))
>>
>> What about?
>>
>> (guard (stringp ,x))
>
> You're suggesting that as a possible alternate syntax, right? (In which
> case I like it.) It doesn't actually work now, does it?

Yes, apologies, my post wasn't very clear.

Phil



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2016-03-03 12:08 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-02 10:12 Quick pcase question Eric Abrahamsen
2016-03-02 11:02 ` Michael Heerdegen
2016-03-02 11:18   ` Eric Abrahamsen
2016-03-02 11:38     ` Michael Heerdegen
2016-03-02 11:56       ` Eric Abrahamsen
2016-03-02 12:27         ` Michael Heerdegen
2016-03-02 17:25           ` Phillip Lord
2016-03-03  1:13             ` Eric Abrahamsen
2016-03-03  3:47             ` Stefan Monnier
2016-03-02 17:18     ` Phillip Lord
2016-03-03  1:08       ` Eric Abrahamsen
2016-03-03 12:08         ` Phillip Lord
2016-03-02 12:40 ` Phillip Lord

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).