* Keywords as function arguments for control flow
@ 2024-11-28 15:21 Heime via Users list for the GNU Emacs text editor
2024-11-29 6:18 ` Tassilo Horn
2024-11-30 11:47 ` Jean Louis
0 siblings, 2 replies; 7+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2024-11-28 15:21 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor
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)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Keywords as function arguments for control flow
2024-11-28 15:21 Keywords as function arguments for control flow Heime via Users list for the GNU Emacs text editor
@ 2024-11-29 6:18 ` Tassilo Horn
2024-11-29 21:06 ` [External] : " Drew Adams
2024-11-30 11:47 ` Jean Louis
1 sibling, 1 reply; 7+ messages in thread
From: Tassilo Horn @ 2024-11-29 6:18 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor; +Cc: Heime
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: [External] : Re: Keywords as function arguments for control flow
2024-11-29 6:18 ` Tassilo Horn
@ 2024-11-29 21:06 ` Drew Adams
2024-11-29 21:26 ` Heime via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2024-11-29 21:06 UTC (permalink / raw)
To: Tassilo Horn, Heime via Users list for the GNU Emacs text editor; +Cc: Heime
> Your example uses symbols not keywords.
And one can Ask Emacs. This is what the Elisp manual
has to say about keywords:
A symbol whose name starts with a colon (':') is
called a keyword symbol. These symbols automatically
act as constants, and are normally used only by
comparing an unknown symbol with a few specific
alternatives. See "Variables that Never Change".
https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html
That "Variables that Never Change" link takes you here:
https://www.gnu.org/software/emacs/manual/html_node/elisp/Constant-Variables.html
where it says this:
Function: keywordp object ¶
function returns t if object is a symbol whose
name starts with ':', interned in the standard
obarray, and returns nil otherwise.
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: [External] : Re: Keywords as function arguments for control flow
2024-11-29 21:06 ` [External] : " Drew Adams
@ 2024-11-29 21:26 ` Heime via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 7+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2024-11-29 21:26 UTC (permalink / raw)
To: Drew Adams
Cc: Tassilo Horn, Heime via Users list for the GNU Emacs text editor
On Saturday, November 30th, 2024 at 9:06 AM, Drew Adams <drew.adams@oracle.com> wrote:
> > Your example uses symbols not keywords.
Correct. But because there was the suggestion of keywords, I thought
of looking into them as well. It seems that the only difference in
using :this rather than 'this. Symbol checking with pcase is
(pcase some-variable
('this (do-something))
and for keywords it would be
(pcase some-variable
(:this (do-something))
Can one just pass :tabtail as an argument to a function?
In my case, would this be as so?
(poulatuk '(72 :tabtrail :global))
> And one can Ask Emacs. This is what the Elisp manual
> has to say about keywords:
>
> A symbol whose name starts with a colon (':') is
> called a keyword symbol. These symbols automatically
> act as constants, and are normally used only by
> comparing an unknown symbol with a few specific
> alternatives. See "Variables that Never Change".
>
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html
>
> That "Variables that Never Change" link takes you here:
>
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Constant-Variables.html
>
> where it says this:
>
> Function: keywordp object ¶
> function returns t if object is a symbol whose
> name starts with ':', interned in the standard
> obarray, and returns nil otherwise.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Keywords as function arguments for control flow
2024-11-28 15:21 Keywords as function arguments for control flow Heime via Users list for the GNU Emacs text editor
2024-11-29 6:18 ` Tassilo Horn
@ 2024-11-30 11:47 ` Jean Louis
2024-11-30 11:55 ` Heime via Users list for the GNU Emacs text editor
1 sibling, 1 reply; 7+ messages in thread
From: Jean Louis @ 2024-11-30 11:47 UTC (permalink / raw)
To: Heime, Heime via Users list for the GNU Emacs text editor
(defun cuprat (seqr)
(pcase seqr
('global (global-whitespace-mode 1)) ;; if seqr is 'global
(_ (whitespace-mode 1)))) ;; default case for other values
(cuprat 'global)
(cuprat 'other)
Explanation
pcase matches the argument seqr to specific patterns (like 'global).
If seqr is 'global, it applies (global-whitespace-mode 1).
The underscore (_) is a catch-all pattern that matches anything else, so in that case, (whitespace-mode 1) is called.
(defun cuprat (seqr)
(cond
((eq seqr 'global) (global-whitespace-mode 1)) ;; if seqr is 'global
(t (whitespace-mode 1)))) ;; default case for other values
(cuprat 'global)
(cuprat 'other)
Explanation
The cond form evaluates each condition in order.
The first condition checks if seqr is 'global, and if so, it runs (global-whitespace-mode 1).
The (t ...) clause is a fallback that handles all other cases, applying (whitespace-mode 1).
Personally I have not ever used pcase
Jean
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Keywords as function arguments for control flow
2024-11-30 11:47 ` Jean Louis
@ 2024-11-30 11:55 ` Heime via Users list for the GNU Emacs text editor
2024-11-30 14:37 ` Jean Louis
0 siblings, 1 reply; 7+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2024-11-30 11:55 UTC (permalink / raw)
To: Jean Louis; +Cc: Heime via Users list for the GNU Emacs text editor
Sent with Proton Mail secure email.
On Saturday, November 30th, 2024 at 11:47 PM, Jean Louis <bugs@gnu.support> wrote:
> (defun cuprat (seqr)
> (pcase seqr
> ('global (global-whitespace-mode 1)) ;; if seqr is 'global
> (_ (whitespace-mode 1)))) ;; default case for other values
>
> (cuprat 'global)
> (cuprat 'other)
>
>
> Explanation
>
> pcase matches the argument seqr to specific patterns (like 'global).
>
> If seqr is 'global, it applies (global-whitespace-mode 1).
>
> The underscore (_) is a catch-all pattern that matches anything else, so in that case, (whitespace-mode 1) is called.
>
>
> (defun cuprat (seqr)
> (cond
> ((eq seqr 'global) (global-whitespace-mode 1)) ;; if seqr is 'global
> (t (whitespace-mode 1)))) ;; default case for other values
>
> (cuprat 'global)
> (cuprat 'other)
>
>
> Explanation
>
> The cond form evaluates each condition in order.
>
> The first condition checks if seqr is 'global, and if so, it runs (global-whitespace-mode 1).
>
> The (t ...) clause is a fallback that handles all other cases, applying (whitespace-mode 1).
>
>
> Personally I have not ever used pcase - Jean
I have used both. The question is about how to pass keywords
(i.e. with :something) as function arguments.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-11-30 14:37 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-28 15:21 Keywords as function arguments for control flow Heime via Users list for the GNU Emacs text editor
2024-11-29 6:18 ` Tassilo Horn
2024-11-29 21:06 ` [External] : " Drew Adams
2024-11-29 21:26 ` Heime via Users list for the GNU Emacs text editor
2024-11-30 11:47 ` Jean Louis
2024-11-30 11:55 ` Heime via Users list for the GNU Emacs text editor
2024-11-30 14:37 ` Jean Louis
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).