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

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

Re: Keywords as function arguments for control flow


From: Tassilo Horn
Subject: Re: Keywords as function arguments for control flow
Date: Fri, 29 Nov 2024 07:18:29 +0100
User-agent: mu4e 1.12.7; emacs 31.0.50

Heime via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> 
writes:

> How cam one use function arguments that use keywords for 
> applying control operations with pcase or cond?
>
> Consider this as example
>
> (defun cuprat (seqr)
>
>     (if (eq seqr 'global)
>           (global-whitespace-mode 1)
>       (whitespace-mode 1)) )
>
> (cuprat 'global) 

Your example uses symbols not keywords.  Here are three versions which
just return the keywords :global / :not-global instead of activating a
mode just for better experimentation.  You can replace :global and
:not-global with (global-whitespace-mode 1) and (whitespace-mode 1) to
get your actual function.

--8<---------------cut here---------------start------------->8---
(defun cuprat (seqr)
  (cl-case seqr
    (global   :global)
    ;; Fallback case with `otherwise' or `t'.
    (otherwise :not-global)))

(cuprat 'global)
;;=> :global
(cuprat nil)
;;=> :not-global
(cuprat 'foobar)
;;=> :not-global

(defun cuprat2 (seqr)
  (pcase seqr
    ('global :global)
    (t       :not-global)))

(cuprat2 'global)
;;=> :global
(cuprat2 nil)
;;=> :not-global
(cuprat2 'foobar)
;;=> :not-global

(defun cuprat3 (seqr)
  (cond
   ((eq seqr 'global) :global)
   (t                 :not-global)))

(cuprat3 'global)
;;=> :global
(cuprat3 nil)
;;=> :not-global
(cuprat3 'foobar)
;;=> :not-global
--8<---------------cut here---------------end--------------->8---

You can easily go from symbols (global) to keywords (:global) just by
replacing global with :global in which case you can also drop all
quoting, i.e., 'global becomes just :global.

Bye,
Tassilo



reply via email to

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