unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* pcase-dolist
@ 2014-07-11 10:35 Sebastian Wiesner
  2014-07-11 13:47 ` pcase-dolist Stefan Monnier
  0 siblings, 1 reply; 22+ messages in thread
From: Sebastian Wiesner @ 2014-07-11 10:35 UTC (permalink / raw)
  To: emacs-devel

Hi,

I just noticed that pcase.el has a `pcase-dolist' macro, which looks quite useful.

However, unlike `pcase’ and `pcase-let’ it’s neither autoloaded nor documented, which suggests that it is somehow internal and should not be used in Emacs Lisp code.

Is this merely an oversight, or are we not supposed to use this macro in Emacs Lisp code for some reason?

Greetings,
Sebastian Wiesner


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

* Re: pcase-dolist
  2014-07-11 10:35 pcase-dolist Sebastian Wiesner
@ 2014-07-11 13:47 ` Stefan Monnier
  2014-07-11 13:50   ` pcase-dolist Sebastian Wiesner
  0 siblings, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2014-07-11 13:47 UTC (permalink / raw)
  To: Sebastian Wiesner; +Cc: emacs-devel

> I just noticed that pcase.el has a `pcase-dolist' macro, which looks
> quite useful.

Indeed, I like it.

> Is this merely an oversight, or are we not supposed to use this macro in
> Emacs Lisp code for some reason?

It was a lack of decision.  I do use it, and you can use it as well.
Just (require 'pcase) before doing so.

I think pcase-dolist should ultimately be renamed to just `dolist'.


        Stefan



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

* Re: pcase-dolist
  2014-07-11 13:47 ` pcase-dolist Stefan Monnier
@ 2014-07-11 13:50   ` Sebastian Wiesner
  2014-07-11 14:14     ` pcase-dolist Stefan Monnier
  0 siblings, 1 reply; 22+ messages in thread
From: Sebastian Wiesner @ 2014-07-11 13:50 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Is this merely an oversight, or are we not supposed to use this macro in
>> Emacs Lisp code for some reason?
> 
> It was a lack of decision.  I do use it, and you can use it as well.

And it’s not just going away at some time, is it?

> Just (require 'pcase) before doing so.

Obviously.

> I think pcase-dolist should ultimately be renamed to just `dolist'.

I’d like that, but will that happen by making a proper obsolete alias that doesn’t immediately break my code, given that pcase-dolist isn’t “advertised”?




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

* Re: pcase-dolist
  2014-07-11 13:50   ` pcase-dolist Sebastian Wiesner
@ 2014-07-11 14:14     ` Stefan Monnier
  0 siblings, 0 replies; 22+ messages in thread
From: Stefan Monnier @ 2014-07-11 14:14 UTC (permalink / raw)
  To: Sebastian Wiesner; +Cc: emacs-devel

> Is this merely an oversight, or are we not supposed to use this macro in
>>> Emacs Lisp code for some reason?
>> It was a lack of decision.  I do use it, and you can use it as well.
> And it’s not just going away at some time, is it?

No.

>> I think pcase-dolist should ultimately be renamed to just `dolist'.
> I’d like that, but will that happen by making a proper obsolete alias that
> doesn’t immediately break my code, given that pcase-dolist isn’t
> “advertised”?

Yes.  If/when it gets renamed to `dolist', the old name will have the
usual obsolete-alias (which, as usual will only stay there for a few
years/releases, tho).


        Stefan



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

* pcase-dolist
@ 2015-07-08 12:20 Michael Heerdegen
  2015-07-08 13:54 ` pcase-dolist Stefan Monnier
  0 siblings, 1 reply; 22+ messages in thread
From: Michael Heerdegen @ 2015-07-08 12:20 UTC (permalink / raw)
  To: Emacs Development

Hello,

I was recently bitten by unexpected behavior of `pcase-dolist'.
Like `pcase-let' (which is used internally by `pcase-dolist'), AFAICT it
assumes that the pattern always matches, e.g.

(pcase-dolist ((and x (pred < 0)) '(-1 0 1 2))
  (message "%s" x))

prints all members of the sequence.

Apart from the question whether this semantic is useful: with the
current semantic, a name that includes "case" is irritating considering
such a behavior.  Maybe "pdolist" (and "plet", "plet*") would be better
names?

Anyway, an idea that came to my mind more than once: `when-let',
`if-let' should really be `pcase-when-let' , `pcase-if-let'.  They would
be much more useful than the plain versions I think.

Then `pcase-dolist' could be reimplemented using `pcase-when-let'.


Michael.



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

* Re: pcase-dolist
  2015-07-08 12:20 pcase-dolist Michael Heerdegen
@ 2015-07-08 13:54 ` Stefan Monnier
  2015-07-08 17:32   ` pcase-dolist Michael Heerdegen
  0 siblings, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2015-07-08 13:54 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Emacs Development

> (pcase-dolist ((and x (pred < 0)) '(-1 0 1 2))
>   (message "%s" x))
> prints all members of the sequence.

What would you want it to do?

I think it'd be the only other meaningful behavior would be to signal an
error, but that would rarely be useful.

It could also skip those elements that don't match, but then it'd be
more like a "filter" loop so it would need another name.  And it would
then be very tempting/desirable to make it possible to provide a chunk
of code to use when the pattern doesn't match.  So we'd end up with
something like

   (pcase-newdolist LIST
    (PAT1 . BODY1)
    (PAT2 . BODY2)
    ...)

which would just be a shorthand for

   (dolist (x LIST)
     (pcase x
       (PAT1 . BODY1)
       (PAT2 . BODY2)
       ...))

> Apart from the question whether this semantic is useful: with the
> current semantic, a name that includes "case" is irritating considering
> such a behavior.

I completely read the name as "<prefix>-dolist" but you have a good
point: the prefix has "case" in its name, which makes it misleading.

> Maybe "pdolist" (and "plet", "plet*") would be better names?

For pcase-dolist, I think the right name to use is `dolist'.

> Anyway, an idea that came to my mind more than once: `when-let',
> `if-let' should really be `pcase-when-let' , `pcase-if-let'.  They would
> be much more useful than the plain versions I think.

Yes, feel free to change them that way.

> Then `pcase-dolist' could be reimplemented using `pcase-when-let'.

Not sure what you mean: the current semantics of pcase-dolist is the
right one IMNSHO.


        Stefan



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

* Re: pcase-dolist
  2015-07-08 13:54 ` pcase-dolist Stefan Monnier
@ 2015-07-08 17:32   ` Michael Heerdegen
  2015-07-08 18:26     ` pcase-dolist Tassilo Horn
  2015-07-08 19:44     ` pcase-dolist Artur Malabarba
  0 siblings, 2 replies; 22+ messages in thread
From: Michael Heerdegen @ 2015-07-08 17:32 UTC (permalink / raw)
  To: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> It could also skip those elements that don't match

That's what I had expected without thinking too much about it.  But I
agree that we would end with something different.

> For pcase-dolist, I think the right name to use is `dolist'.

Sounds good!

> > Anyway, an idea that came to my mind more than once: `when-let',
> > `if-let' should really be `pcase-when-let' , `pcase-if-let'.  They
> > would be much more useful than the plain versions I think.
>
> Yes, feel free to change them that way.

Though, just changing them would break existing code (the current
versions are about boolean values, the pcase versions would be about
pattern matching).

For example, now

  (if-let ((a (ignore))) a 17)       ==> 17,

but

  (pcase-if-let ((a (ignore))) a 17) ==> nil.


Michael.




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

* Re: pcase-dolist
  2015-07-08 17:32   ` pcase-dolist Michael Heerdegen
@ 2015-07-08 18:26     ` Tassilo Horn
  2015-07-08 21:27       ` pcase-dolist Stefan Monnier
  2015-07-08 19:44     ` pcase-dolist Artur Malabarba
  1 sibling, 1 reply; 22+ messages in thread
From: Tassilo Horn @ 2015-07-08 18:26 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: emacs-devel

Michael Heerdegen <michael_heerdegen@web.de> writes:

>> It could also skip those elements that don't match
>
> That's what I had expected without thinking too much about it.  But I
> agree that we would end with something different.

I also think that `pcase-let' and `pcase-dolist' are pretty confusing.
For example,

  (pcase-let ((`(,w ,x ,y ,z) '(1 2 3)))
    (message "%s %s %s %s" w x y z))

prints "1 2 3 nil" although the pattern when used in `pcase' wouldn't
match that list but only 4-element lists.  So a pcase pattern has
different semantics depending on whether it is used in `pcase' or
`pcase-let' or `pcase-dolist' which doesn't feel right.

I'd prefer if `pcase-let' would signal an error if some pattern doesn't
match.  Same for `pcase-dolist' with its current semantics.  IMHO, the
code above clearly says that the programmer expected to destructure
4-element lists so an error signaling that the expectation is wrong
sounds right.

>> > Anyway, an idea that came to my mind more than once: `when-let',
>> > `if-let' should really be `pcase-when-let' , `pcase-if-let'.  They
>> > would be much more useful than the plain versions I think.
>>
>> Yes, feel free to change them that way.
>
> Though, just changing them would break existing code (the current
> versions are about boolean values, the pcase versions would be about
> pattern matching).

So why not have both?

Bye,
Tassilo



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

* Re: pcase-dolist
  2015-07-08 17:32   ` pcase-dolist Michael Heerdegen
  2015-07-08 18:26     ` pcase-dolist Tassilo Horn
@ 2015-07-08 19:44     ` Artur Malabarba
  2015-07-08 20:50       ` pcase-dolist Michael Heerdegen
  1 sibling, 1 reply; 22+ messages in thread
From: Artur Malabarba @ 2015-07-08 19:44 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: emacs-devel

>> > Anyway, an idea that came to my mind more than once: `when-let',
>> > `if-let' should really be `pcase-when-let' , `pcase-if-let'.  They
>> > would be much more useful than the plain versions I think.
>>
>> Yes, feel free to change them that way.
>
> Though, just changing them would break existing code (the current
> versions are about boolean values, the pcase versions would be about
> pattern matching).
>
> For example, now
>
>   (if-let ((a (ignore))) a 17)       ==> 17,
>
> but
>
>   (pcase-if-let ((a (ignore))) a 17) ==> nil.

Only if you make them like this. The idea of `(if-let ((a expr))
body)' is that evaluate `expr' and, if it is non-nil, bind it to a and
run `body'. This doesn't exclude the possibility of `a' being a pcase
pattern. Just make sure that the the check for nil expr is done as a
separate thing, before the pattern matching.

If you implement pcase-if-let the way you suggest above, isn't that
just the same as pcase?



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

* Re: pcase-dolist
  2015-07-08 19:44     ` pcase-dolist Artur Malabarba
@ 2015-07-08 20:50       ` Michael Heerdegen
  0 siblings, 0 replies; 22+ messages in thread
From: Michael Heerdegen @ 2015-07-08 20:50 UTC (permalink / raw)
  To: emacs-devel

Artur Malabarba <bruce.connor.am@gmail.com> writes:

> Only if you make them like this. The idea of `(if-let ((a expr))
> body)' is that evaluate `expr' and, if it is non-nil, bind it to a and
> run `body'. This doesn't exclude the possibility of `a' being a pcase
> pattern. Just make sure that the the check for nil expr is done as a
> separate thing, before the pattern matching.

I don't know if I would like that semantic.  It doesn't sound intuitive
to me.

> If you implement pcase-if-let the way you suggest above, isn't that
> just the same as pcase?

For one binding, yes.  The body would be at a more prominent place
though.

Isn't if-let not just pcase as well?

  (if-let ((var expr)) then else)

  (pcase expr ((and var (guard var)) then) (_ else))


Michael.




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

* Re: pcase-dolist
  2015-07-08 18:26     ` pcase-dolist Tassilo Horn
@ 2015-07-08 21:27       ` Stefan Monnier
  2015-07-09  6:05         ` pcase-dolist Tassilo Horn
  0 siblings, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2015-07-08 21:27 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: emacs-devel

>   (pcase-let ((`(,w ,x ,y ,z) '(1 2 3)))
>     (message "%s %s %s %s" w x y z))

> prints "1 2 3 nil" although the pattern when used in `pcase' wouldn't
> match that list but only 4-element lists.  So a pcase pattern has
> different semantics depending on whether it is used in `pcase' or
> `pcase-let' or `pcase-dolist' which doesn't feel right.

> I'd prefer if `pcase-let' would signal an error if some pattern doesn't
> match.

So you'd want the above to signal an error in the case of:

   (pcase-let ((`(,w ,x) '(1 2 3)))
     (message "%s %s" w x))

?


        Stefan



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

* Re: pcase-dolist
  2015-07-08 21:27       ` pcase-dolist Stefan Monnier
@ 2015-07-09  6:05         ` Tassilo Horn
  2015-07-09  9:55           ` pcase-dolist Thierry Volpiatto
  2015-07-09 19:26           ` pcase-dolist Stefan Monnier
  0 siblings, 2 replies; 22+ messages in thread
From: Tassilo Horn @ 2015-07-09  6:05 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Michael Heerdegen, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>   (pcase-let ((`(,w ,x ,y ,z) '(1 2 3)))
>>     (message "%s %s %s %s" w x y z))
>
>> prints "1 2 3 nil" although the pattern when used in `pcase' wouldn't
>> match that list but only 4-element lists.  So a pcase pattern has
>> different semantics depending on whether it is used in `pcase' or
>> `pcase-let' or `pcase-dolist' which doesn't feel right.
>
>> I'd prefer if `pcase-let' would signal an error if some pattern doesn't
>> match.
>
> So you'd want the above to signal an error in the case of:
>
>    (pcase-let ((`(,w ,x) '(1 2 3)))
>      (message "%s %s" w x))
>
> ?

Yes, if you want to pick the first two elements of a list with 2 or more
elements, you should use `(,w ,x . ,_).  I see that the above is
slightly more convenient and concise but I'd value consistency more.

Bye,
Tassilo



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

* Re: pcase-dolist
  2015-07-09  6:05         ` pcase-dolist Tassilo Horn
@ 2015-07-09  9:55           ` Thierry Volpiatto
  2015-07-09 10:17             ` pcase-dolist Tassilo Horn
  2015-07-09 19:26           ` pcase-dolist Stefan Monnier
  1 sibling, 1 reply; 22+ messages in thread
From: Thierry Volpiatto @ 2015-07-09  9:55 UTC (permalink / raw)
  To: emacs-devel

Tassilo Horn <tsdh@gnu.org> writes:

> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>>>   (pcase-let ((`(,w ,x ,y ,z) '(1 2 3)))
>>>     (message "%s %s %s %s" w x y z))
>>
>>> prints "1 2 3 nil" although the pattern when used in `pcase' wouldn't
>>> match that list but only 4-element lists.  So a pcase pattern has
>>> different semantics depending on whether it is used in `pcase' or
>>> `pcase-let' or `pcase-dolist' which doesn't feel right.
>>
>>> I'd prefer if `pcase-let' would signal an error if some pattern doesn't
>>> match.
>>
>> So you'd want the above to signal an error in the case of:
>>
>>    (pcase-let ((`(,w ,x) '(1 2 3)))
>>      (message "%s %s" w x))
>>
>> ?
>
> Yes, if you want to pick the first two elements of a list with 2 or more
> elements, you should use `(,w ,x . ,_).  I see that the above is
> slightly more convenient and concise but I'd value consistency more.

This would be a regression IMHO, what if you want to do e.g:

(pcase-let ((`(,a ,b) (file-attributes "foo")))
  (list a b))

It would be inconvenient to have to bind the 10 unused remaining elements.

Of course one can use `cl-multiple-value-bind' instead in such situation.

-- 
Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




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

* Re: pcase-dolist
  2015-07-09  9:55           ` pcase-dolist Thierry Volpiatto
@ 2015-07-09 10:17             ` Tassilo Horn
  2015-07-09 12:19               ` pcase-dolist Thierry Volpiatto
  0 siblings, 1 reply; 22+ messages in thread
From: Tassilo Horn @ 2015-07-09 10:17 UTC (permalink / raw)
  To: Thierry Volpiatto; +Cc: emacs-devel

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

>>> So you'd want the above to signal an error in the case of:
>>>
>>>    (pcase-let ((`(,w ,x) '(1 2 3)))
>>>      (message "%s %s" w x))
>>>
>>> ?
>>
>> Yes, if you want to pick the first two elements of a list with 2 or
>> more elements, you should use `(,w ,x . ,_).  I see that the above is
>> slightly more convenient and concise but I'd value consistency more.
>
> This would be a regression IMHO, what if you want to do e.g:
>
> (pcase-let ((`(,a ,b) (file-attributes "foo")))
>   (list a b))
>
> It would be inconvenient to have to bind the 10 unused remaining
> elements.

As I've said, you can use cdr-matching with a don't-care-pattern, e.g.,

  (pcase-let ((`(,a ,b . ,_) (file-attributes "~/.emacs")))
    (list a b))

which clearly states that you know that the list returned by
`file-attributes' has more than two elements but you are only interested
in the first two.

Bye,
Tassilo



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

* Re: pcase-dolist
  2015-07-09 10:17             ` pcase-dolist Tassilo Horn
@ 2015-07-09 12:19               ` Thierry Volpiatto
  2015-07-09 13:34                 ` pcase-dolist Tassilo Horn
  0 siblings, 1 reply; 22+ messages in thread
From: Thierry Volpiatto @ 2015-07-09 12:19 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-devel


Tassilo Horn <tsdh@gnu.org> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>>>> So you'd want the above to signal an error in the case of:
>>>>
>>>>    (pcase-let ((`(,w ,x) '(1 2 3)))
>>>>      (message "%s %s" w x))
>>>>
>>>> ?
>>>
>>> Yes, if you want to pick the first two elements of a list with 2 or
>>> more elements, you should use `(,w ,x . ,_).  I see that the above is
>>> slightly more convenient and concise but I'd value consistency more.
>>
>> This would be a regression IMHO, what if you want to do e.g:
>>
>> (pcase-let ((`(,a ,b) (file-attributes "foo")))
>>   (list a b))
>>
>> It would be inconvenient to have to bind the 10 unused remaining
>> elements.
>
> As I've said, you can use cdr-matching with a don't-care-pattern, e.g.,
>
>   (pcase-let ((`(,a ,b . ,_) (file-attributes "~/.emacs")))
>     (list a b))
>
> which clearly states that you know that the list returned by
> `file-attributes' has more than two elements but you are only interested
> in the first two.

Ah! Ok so the dont-care-pattern handle all the remaining elements.
So yes like this the intention is clearer.

Thanks.

-- 
Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 



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

* Re: pcase-dolist
  2015-07-09 12:19               ` pcase-dolist Thierry Volpiatto
@ 2015-07-09 13:34                 ` Tassilo Horn
  2015-07-09 13:40                   ` pcase-dolist Thierry Volpiatto
  0 siblings, 1 reply; 22+ messages in thread
From: Tassilo Horn @ 2015-07-09 13:34 UTC (permalink / raw)
  To: Thierry Volpiatto; +Cc: emacs-devel

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

>> As I've said, you can use cdr-matching with a don't-care-pattern,
>> e.g.,
>>
>>   (pcase-let ((`(,a ,b . ,_) (file-attributes "~/.emacs")))
>>     (list a b))
>>
>> which clearly states that you know that the list returned by
>> `file-attributes' has more than two elements but you are only interested
>> in the first two.
>
> Ah! Ok so the dont-care-pattern handle all the remaining elements.

No, the crucial part is the dot separating the front from the rest of
the list.  With `(,a ,b . ,r) the list of the 8 remaining file
attributes would be bound to r.  When you don't need those, you can omit
the binding with the don't care pattern.

Bye,
Tassilo



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

* Re: pcase-dolist
  2015-07-09 13:34                 ` pcase-dolist Tassilo Horn
@ 2015-07-09 13:40                   ` Thierry Volpiatto
  0 siblings, 0 replies; 22+ messages in thread
From: Thierry Volpiatto @ 2015-07-09 13:40 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-devel


Tassilo Horn <tsdh@gnu.org> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>>> As I've said, you can use cdr-matching with a don't-care-pattern,
>>> e.g.,
>>>
>>>   (pcase-let ((`(,a ,b . ,_) (file-attributes "~/.emacs")))
>>>     (list a b))
>>>
>>> which clearly states that you know that the list returned by
>>> `file-attributes' has more than two elements but you are only interested
>>> in the first two.
>>
>> Ah! Ok so the dont-care-pattern handle all the remaining elements.
>
> No, the crucial part is the dot separating the front from the rest of
> the list.  With `(,a ,b . ,r) the list of the 8 remaining file
> attributes would be bound to r.  When you don't need those, you can omit
> the binding with the don't care pattern.

Yes it's what I understood.

Thanks.

-- 
Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 



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

* Re: pcase-dolist
  2015-07-09  6:05         ` pcase-dolist Tassilo Horn
  2015-07-09  9:55           ` pcase-dolist Thierry Volpiatto
@ 2015-07-09 19:26           ` Stefan Monnier
  2015-07-09 20:27             ` pcase-dolist Michael Heerdegen
  2015-07-10 14:44             ` pcase-dolist Tassilo Horn
  1 sibling, 2 replies; 22+ messages in thread
From: Stefan Monnier @ 2015-07-09 19:26 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: emacs-devel

> Yes, if you want to pick the first two elements of a list with 2 or more
> elements, you should use `(,w ,x . ,_).  I see that the above is
> slightly more convenient and concise but I'd value consistency more.

But that does not match the usual uses of "tuples represented as lists",
where it's customary to use (A B) when the remaining fields are all nil
(since (car nil) returns nil and (nth 5 '(1 2)) also returns nil rather
than signaling an error) and where it's also customary to ignore any
additional element.

IOW maybe your approach seems more consistent, but not only does it
generate less efficient code, but it also requires extra source code in
the usual cases.


        Stefan



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

* Re: pcase-dolist
  2015-07-09 19:26           ` pcase-dolist Stefan Monnier
@ 2015-07-09 20:27             ` Michael Heerdegen
  2015-07-10 18:51               ` pcase-dolist Stefan Monnier
  2015-07-10 14:44             ` pcase-dolist Tassilo Horn
  1 sibling, 1 reply; 22+ messages in thread
From: Michael Heerdegen @ 2015-07-09 20:27 UTC (permalink / raw)
  To: emacs-devel

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

> But that does not match the usual uses of "tuples represented as
> lists", where it's customary to use (A B) when the remaining fields
> are all nil (since (car nil) returns nil and (nth 5 '(1 2)) also
> returns nil rather than signaling an error) and where it's also
> customary to ignore any additional element.

Good argument.  OTOH, if you _want_ to distinguish e.g. a two from a
three element list, you will have to use something like `(,a ,b . ,r)
and test whether r is nil.  Mmh.  Anyway, pcase and pcase-let should
at least behave the same at the end I think...


Michael.




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

* Re: pcase-dolist
  2015-07-09 19:26           ` pcase-dolist Stefan Monnier
  2015-07-09 20:27             ` pcase-dolist Michael Heerdegen
@ 2015-07-10 14:44             ` Tassilo Horn
  2015-07-10 19:04               ` pcase-dolist Stefan Monnier
  1 sibling, 1 reply; 22+ messages in thread
From: Tassilo Horn @ 2015-07-10 14:44 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Michael Heerdegen, emacs-devel

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

>> Yes, if you want to pick the first two elements of a list with 2 or
>> more elements, you should use `(,w ,x . ,_).  I see that the above is
>> slightly more convenient and concise but I'd value consistency more.
>
> But that does not match the usual uses of "tuples represented as
> lists", where it's customary to use (A B) when the remaining fields
> are all nil (since (car nil) returns nil and (nth 5 '(1 2)) also
> returns nil rather than signaling an error) and where it's also
> customary to ignore any additional element.

Yes, but those don't have pcase in their name.  If that wouldn't be the
case with `pcase-let' I would not complain at all.

Hm, when you said that a better name for `pcase-dolist' was `dolist',
wouldn't `let(*)' be a better name for `pcase-let(*)', too?  It would be
very useful to be able to destructure in usual lets (like, e.g., in
Clojure).  I think there are gazillion of occurrences of

  (let* ((this-and-that (foo ...))
         (this (car this-and-that))
         (that (cdr this-and-that)))
    ...)

AFAICS, the only thing that `pcase-let' is lacking is the ability to
introduce locals without providing a value, e.g.,

  (let ((x 1) b c) ...)

but that shouldn't be too hard to add, no?

Bye,
Tassilo



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

* Re: pcase-dolist
  2015-07-09 20:27             ` pcase-dolist Michael Heerdegen
@ 2015-07-10 18:51               ` Stefan Monnier
  0 siblings, 0 replies; 22+ messages in thread
From: Stefan Monnier @ 2015-07-10 18:51 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: emacs-devel

> Good argument.  OTOH, if you _want_ to distinguish e.g. a two from a
> three element list, you will have to use something like `(,a ,b . ,r)
> and test whether r is nil.

No: pcase-let is for "destructuring", not for "distinguishing".
IOW if you want "to distinguish e.g. a two from a three element list"
you want to use `pcase'.

> Mmh.  Anyway, pcase and pcase-let should
> at least behave the same at the end I think...

Then what would be the benefit of having `pcase-let'?


        Stefan



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

* Re: pcase-dolist
  2015-07-10 14:44             ` pcase-dolist Tassilo Horn
@ 2015-07-10 19:04               ` Stefan Monnier
  0 siblings, 0 replies; 22+ messages in thread
From: Stefan Monnier @ 2015-07-10 19:04 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: emacs-devel

> Yes, but those don't have pcase in their name.

As mentioned earlier, I agree that the name is misleading (and it didn't
occur to me, because to me "pcase-" is just a prefix) this name is only
used because pcase-let uses the same pattern syntax and underlying
machinery as `pcase'.  We could rename it to `plet' (or even to just
`let').

> Hm, when you said that a better name for `pcase-dolist' was `dolist',
> wouldn't `let(*)' be a better name for `pcase-let(*)', too?

Yes.  2 reasons why it's not done:
- just as for dolist, I haven't made that jump (yet?), it's one of those
  things where there's no going back, so I need to work up my courage
  and convince myself it's worthwhile.
- since `let' is currently a builtin element, and used in the expansion
  of pcase-let, there are some technical details that'd need to be
  resolved (the technical issues for dolist are trivial in comparison).

> It would be very useful to be able to destructure in usual lets (like,
> e.g., in Clojure).

That's why I introduced pcase-let.

> AFAICS, the only thing that `pcase-let' is lacking is the ability to
> introduce locals without providing a value, e.g.,
>   (let ((x 1) b c) ...)

I tend to dislike these things, so I intentionally didn't add support
for them in pcase-let, but it'd be trivial to support them.


        Stefan



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

end of thread, other threads:[~2015-07-10 19:04 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-11 10:35 pcase-dolist Sebastian Wiesner
2014-07-11 13:47 ` pcase-dolist Stefan Monnier
2014-07-11 13:50   ` pcase-dolist Sebastian Wiesner
2014-07-11 14:14     ` pcase-dolist Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2015-07-08 12:20 pcase-dolist Michael Heerdegen
2015-07-08 13:54 ` pcase-dolist Stefan Monnier
2015-07-08 17:32   ` pcase-dolist Michael Heerdegen
2015-07-08 18:26     ` pcase-dolist Tassilo Horn
2015-07-08 21:27       ` pcase-dolist Stefan Monnier
2015-07-09  6:05         ` pcase-dolist Tassilo Horn
2015-07-09  9:55           ` pcase-dolist Thierry Volpiatto
2015-07-09 10:17             ` pcase-dolist Tassilo Horn
2015-07-09 12:19               ` pcase-dolist Thierry Volpiatto
2015-07-09 13:34                 ` pcase-dolist Tassilo Horn
2015-07-09 13:40                   ` pcase-dolist Thierry Volpiatto
2015-07-09 19:26           ` pcase-dolist Stefan Monnier
2015-07-09 20:27             ` pcase-dolist Michael Heerdegen
2015-07-10 18:51               ` pcase-dolist Stefan Monnier
2015-07-10 14:44             ` pcase-dolist Tassilo Horn
2015-07-10 19:04               ` pcase-dolist Stefan Monnier
2015-07-08 19:44     ` pcase-dolist Artur Malabarba
2015-07-08 20:50       ` pcase-dolist Michael Heerdegen

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