all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Advice on writing predicates
@ 2009-06-25 22:27 Sarir Khamsi
  2009-06-25 23:23 ` Anselm Helbig
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Sarir Khamsi @ 2009-06-25 22:27 UTC (permalink / raw)
  To: help-gnu-emacs

I wrote a simple predicate that returns t if the Emacs major version
number is greater than 22 and would like some advice/comments:

The following

(defun sk-emacs-version-greater-than23-p ()
  "Return non-nil if current Emacs version is greater than 22."
  (interactive)
  (setq version-string (replace-regexp-in-string ".*?\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)[()-.a-zA-Z0-9 \n]+" "\\1 \\2 \\3" (emacs-version)))
  (setq version-num (mapcar 'string-to-number (split-string version-string)))
  (if (> 22 (car version-num))
      t
    nil))

seems to work. I know that I don't need to save \2 and \3 but wanted
that for a later function. Any comments or suggestions on how to make
this better? Thanks.

Sarir

-- 
Sarir Khamsi
software guy
sarir.khamsi@raytheon.com


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

* Re: Advice on writing predicates
  2009-06-25 22:27 Advice on writing predicates Sarir Khamsi
@ 2009-06-25 23:23 ` Anselm Helbig
  2009-06-29 19:18   ` chickenphat
  2009-06-26  6:40 ` Daniel Pittman
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 7+ messages in thread
From: Anselm Helbig @ 2009-06-25 23:23 UTC (permalink / raw)
  To: help-gnu-emacs

At Thu, 25 Jun 2009 15:27:19 -0700,
Sarir Khamsi <sarir.khamsi@raytheon.com> wrote:
> 
> I wrote a simple predicate that returns t if the Emacs major version
> number is greater than 22 and would like some advice/comments:
> 
> The following
> 
> (defun sk-emacs-version-greater-than23-p ()
>   "Return non-nil if current Emacs version is greater than 22."
>   (interactive)
>   (setq version-string (replace-regexp-in-string ".*?\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)[()-.a-zA-Z0-9 \n]+" "\\1 \\2 \\3" (emacs-version)))
>   (setq version-num (mapcar 'string-to-number (split-string version-string)))
>   (if (> 22 (car version-num))
>       t
>     nil))

I hope you're not too disappointed, but there's a constant named
emacs-major-version: 

  (> emacs-major-version 22)

This is also extracted from the string in emacs-version, this is how
the emacs devs did it:

  (defconst emacs-major-version
    (progn (string-match "^[0-9]+" emacs-version)
     (string-to-number (match-string 0 emacs-version)))
    "Major version number of this version of Emacs.
  This variable first existed in version 19.23.")

If you want to parse the version number yourself, this is how I would
do it: 

  (mapcar 'string-to-number (split-string emacs-version "\\."))

Note that the emacs-version variable just holds the number. This makes
things a lot easier. 

Generally you shouldnt rely on the version number to test if a feature
is available - just test directly for the feature you need. 

Regards, 

Anselm


-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


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

* Re: Advice on writing predicates
  2009-06-25 22:27 Advice on writing predicates Sarir Khamsi
  2009-06-25 23:23 ` Anselm Helbig
@ 2009-06-26  6:40 ` Daniel Pittman
  2009-06-26  7:37 ` Andreas Röhler
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Daniel Pittman @ 2009-06-26  6:40 UTC (permalink / raw)
  To: help-gnu-emacs

Sarir Khamsi <sarir.khamsi@raytheon.com> writes:

> I wrote a simple predicate that returns t if the Emacs major version number
> is greater than 22 and would like some advice/comments:
>
> The following
>
> (defun sk-emacs-version-greater-than23-p ()
>   "Return non-nil if current Emacs version is greater than 22."
>   (interactive)
>   (setq version-string (replace-regexp-in-string ".*?\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)[()-.a-zA-Z0-9 \n]+" "\\1 \\2 \\3" (emacs-version)))
>   (setq version-num (mapcar 'string-to-number (split-string version-string)))
>   (if (> 22 (car version-num))
>       t
>     nil))

(defun dp/emacs-version-greater-than-23-p ()
  "REVISIT: Document me, you fool!"
  (interactive)
  (> (string-to-number (car (split-string emacs-version "\\."))) 23))

That does two things better: first, it uses the emacs-version variable which
just holds the version number.

Second, it doesn't bother with a specific true value, just a generic one,
which the '>' function happily returns.

Regards,
        Daniel





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

* Re: Advice on writing predicates
  2009-06-25 22:27 Advice on writing predicates Sarir Khamsi
  2009-06-25 23:23 ` Anselm Helbig
  2009-06-26  6:40 ` Daniel Pittman
@ 2009-06-26  7:37 ` Andreas Röhler
  2009-06-26  9:13 ` Pascal J. Bourguignon
  2009-06-26 11:02 ` Thien-Thi Nguyen
  4 siblings, 0 replies; 7+ messages in thread
From: Andreas Röhler @ 2009-06-26  7:37 UTC (permalink / raw)
  To: Sarir Khamsi

Sarir Khamsi wrote:
> I wrote a simple predicate that returns t if the Emacs major version
> number is greater than 22 and would like some advice/comments:
>
> The following
>
> (defun sk-emacs-version-greater-than23-p ()
>   "Return non-nil if current Emacs version is greater than 22."
>   (interactive)
>   (setq version-string (replace-regexp-in-string ".*?\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)[()-.a-zA-Z0-9 \n]+" "\\1 \\2 \\3" (emacs-version)))
>   (setq version-num (mapcar 'string-to-number (split-string version-string)))
>   (if (> 22 (car version-num))
>       t
>     nil))
>
> seems to work. I know that I don't need to save \2 and \3 but wanted
> that for a later function.

After the core question has been answered, remains: how to deliver values?

Several possibilities exist. You could return a list instead a boolean.

Or define variables outside:

(defvar value-to-preserve1 nil)
(defvar value-to-preserve2 nil)

(defun sk-emacs-version-greater-than23-p ()
  "Return non-nil if current Emacs version is greater than 22."
  (interactive)
  (setq version-string (replace-regexp-in-string
".*?\\([0-9]+\\)\.\\([0-9]+\\)\.\\([0-9]+\\)[()-.a-zA-Z0-9 \n]+" "\\1
\\2 \\3" (emacs-version)))
(setq version-num (mapcar 'string-to-number (split-string version-string)))
;;;;;;;;;;
  (setq value-to-preserve1 (nth 1 version-num))
  (setq value-to-preserve2 (nth 2 version-num))
::::::::::
  (if (> 22 (car version-num))
      t
    nil))


Cheers

Andreas


>  Any comments or suggestions on how to make
> this better? Thanks.
>
> Sarir
>
>   





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

* Re: Advice on writing predicates
  2009-06-25 22:27 Advice on writing predicates Sarir Khamsi
                   ` (2 preceding siblings ...)
  2009-06-26  7:37 ` Andreas Röhler
@ 2009-06-26  9:13 ` Pascal J. Bourguignon
  2009-06-26 11:02 ` Thien-Thi Nguyen
  4 siblings, 0 replies; 7+ messages in thread
From: Pascal J. Bourguignon @ 2009-06-26  9:13 UTC (permalink / raw)
  To: help-gnu-emacs

Sarir Khamsi <sarir.khamsi@raytheon.com> writes:

> I wrote a simple predicate that returns t if the Emacs major version
> number is greater than 22 and would like some advice/comments:
> [...]
>
>   (if (> 22 (car version-num))
>       t
>     nil))
>
> seems to work. I know that I don't need to save \2 and \3 but wanted
> that for a later function. Any comments or suggestions on how to make
> this better? Thanks.

1- anything that is not nil is true, so you don't really need to
   return t, just something not nil.

2- > already returns either t or nil.  So you can just write 
   (> 22 emacs-major-version).

3- even if > didn't return t for true, you could just write
   (> 22 emacs-major-version) (see point 1).

   An famous example of a function that doesn't return t or nil, but a
   generalized boolean is member:

     (member 'b '(a b c)) --> (b c) ; which is true.

   So you can write either:

     (defun containp  (x l) (member x l)) ; returns a generalized boolean
     (defun following (x l) (second (member x l))) ; returns the element following x in l.

     (let ((list '(a b c))
           (e    'b))
        (when (containp e list)
           (following e list)))
     --> c

4- but if you really wanted to return t or nil in case 3, you could
   write instead of if: (not (not (> 22 emacs-major-verion))).
   Which could be rewritten as (not (<= 22 emacs-major-verion)).

5- one of my teacher taught us to always use < or <=, never > or >=,
   so you get to write always the smallest on the left and the biggest
   on the right, which is more readable, in the long term.

In conclusion, you could write it better as:

   (< emacs-major-version 22)


-- 
__Pascal Bourguignon__


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

* Re: Advice on writing predicates
  2009-06-25 22:27 Advice on writing predicates Sarir Khamsi
                   ` (3 preceding siblings ...)
  2009-06-26  9:13 ` Pascal J. Bourguignon
@ 2009-06-26 11:02 ` Thien-Thi Nguyen
  4 siblings, 0 replies; 7+ messages in thread
From: Thien-Thi Nguyen @ 2009-06-26 11:02 UTC (permalink / raw)
  To: help-gnu-emacs

() Sarir Khamsi <sarir.khamsi@raytheon.com>
() Thu, 25 Jun 2009 15:27:19 -0700

   (defun sk-emacs-version-greater-than23-p ()
     [...])

   Any comments or suggestions on how to make
   this better?

Other people have answered otherwise, so i'll just say:
avoid setq to free vars.

thi




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

* Re: Advice on writing predicates
  2009-06-25 23:23 ` Anselm Helbig
@ 2009-06-29 19:18   ` chickenphat
  0 siblings, 0 replies; 7+ messages in thread
From: chickenphat @ 2009-06-29 19:18 UTC (permalink / raw)
  To: help-gnu-emacs

On Jun 25, 4:23 pm, Anselm Helbig <anselm.helbig
+news2...@googlemail.com> wrote:
> At Thu, 25 Jun 2009 15:27:19 -0700,
>
> I hope you're not too disappointed, but there's a constant named
> emacs-major-version:
>
>   (> emacs-major-version 22)
>
> This is also extracted from the string in emacs-version, this is how
> the emacs devs did it:
>
>   (defconst emacs-major-version
>     (progn (string-match "^[0-9]+" emacs-version)
>      (string-to-number (match-string 0 emacs-version)))
>     "Major version number of this version of Emacs.
>   This variable first existed in version 19.23.")
>
> If you want to parse the version number yourself, this is how I would
> do it:
>
>   (mapcar 'string-to-number (split-string emacs-version "\\."))
>
> Note that the emacs-version variable just holds the number. This makes
> things a lot easier.

This is exactly what I was looking for...and I'm very happy. Thanks.

Sarir


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

end of thread, other threads:[~2009-06-29 19:18 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-25 22:27 Advice on writing predicates Sarir Khamsi
2009-06-25 23:23 ` Anselm Helbig
2009-06-29 19:18   ` chickenphat
2009-06-26  6:40 ` Daniel Pittman
2009-06-26  7:37 ` Andreas Röhler
2009-06-26  9:13 ` Pascal J. Bourguignon
2009-06-26 11:02 ` Thien-Thi Nguyen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.