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

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

bug#31311: 27.0; doc of `pcase'


From: Noam Postavsky
Subject: bug#31311: 27.0; doc of `pcase'
Date: Thu, 24 May 2018 19:13:40 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Thien-Thi Nguyen <ttn@gnu.org> writes:

> Also, ‘pcase’ is the only conditional construct that takes a
> first arg and evaluates it (like ‘case’).  The new text explains
> its hybrid nature (‘cond’ and ‘case’) in more detail.

The manual should refer to `cl-case', not `case'.  I don't think pcase
should be referred to as a hybrid of cond and cl-case; maybe a hybrid of
cond and cl-destructuring-bind.  Or perhaps: like cl-case, only more so.

> +These render @code{case} unsuitable for strings or compound
> +data structures (e.g., lists or vectors).
> +For that, why not use @code{cond}?  Wait, what?  @dots{}

I guess this is supposed to be funny, but I think it might confuse the
reader.

> +(defun grok/traditional (obj)
> +  (if (and (stringp obj)
> +           (string-match "^key:\\([[:digit:]]+\\)$" obj))
> +      (match-string 1 obj)
> +    (list "149" 'default)))

> +(defun grok/pcase (obj)
> +  (pcase obj
> +    ((or                                     ; @r{line 1}
> +      (and                                   ; @r{line 2}
> +       (pred stringp)                        ; @r{line 3}
> +       (pred (string-match                   ; @r{line 4}
> +              "^key:\\([[:digit:]]+\\)$"))   ; @r{line 5}
> +       (app (match-string 1)                 ; @r{line 6}

You have to pass the original string to match-string.

> +            val))                            ; @r{line 7}
> +      (let val (list "149" 'default)))       ; @r{line 8}
> +     val)))                                  ; @r{line 9}

This doesn't seem like a great example of pcase usage.  Not sure if you
want to introduce the extended `rx' pattern here, but it works better
for this, IMO:

    (pcase x
      ((and (pred stringp)
            (rx bol "key:" (let val (+ (any digit))) eol))
       val)
      (_ 'default))





reply via email to

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