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: Jean Louis
Subject: Re: Keywords as function arguments for control flow
Date: Sat, 30 Nov 2024 14:47:36 +0300
User-agent: K-9 Mail for Android

(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



reply via email to

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