[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to remove verbosity from the data passing mechanism using alist
From: |
Thien-Thi Nguyen |
Subject: |
Re: How to remove verbosity from the data passing mechanism using alist or plist ? |
Date: |
Mon, 06 Dec 2010 15:13:49 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) |
[cc changed to help-gnu-emacs -- this does not belong on emacs-devel]
() Fren Zeee <frenzeee@gmail.com>
() Sun, 5 Dec 2010 12:15:08 -0800
[code]
Here is ‘find-my-marker-GOLD’, munged a bit:
(defun find-my-marker-GOLD ()
"Starting from anywhere in the file, find my marker GOLD its value
and location."
(interactive)
(save-excursion
(save-match-data
;; Starting from the end of the accessible region, find GOLD.
(goto-char (point-max))
;; a/ This value of this expression is discarded.
;; It turns out to be the same as "location" below,
;; so if you save it, you can avoid a call to ‘point’.
;; b/ The optional args default to ‘nil’, and can be dropped.
(search-backward-regexp "GOLD=\\([0-9]+\\)\n" nil nil nil)
;; In the following, i have deleted the eol comments, which
;; obscure more than enlighten (on my small computer screen).
;; You might consider using `((k0 . ,v0)
;; (k1 . ,v1))
;; for succinctness (note backquote and comma placement).
;; An intermediate solution is to use ‘acons’.
(list
(list
:GOLD-value
;; The ‘replace-regexp-in-string’ is not necessary.
;; The initial regexp match (above) already sets the match
;; data; you can use ‘(match-string 1)’ to retrieve it.
(string-to-number
(replace-regexp-in-string "GOLD=\\([0-9]+\\)\n" "\\1" (match-string
0))))
(list
:GOLD-location
(point))))))
[Q] Are there any defects in this method of passing struct and what
improvements are possible ?
All defects are misalignments of intention and implementation.
If you don't know your intention clearly, it's easy for a defect
to creep in. Improvements, likewise, depend on intention and pov.
Specifically, are there ways to reduce verbosity without using cl
or staying purely in elisp ?
You can use shorter variable names. You can make your program
less piecewise-constructive and more table-oriented.
[Q] Is there a way to avoid lengthy calling statement like
(car (assoc-default :GOLD-value GOLD )
inside let,
since the first argument of let is an alist of the form
((sym1 val1) (sym2 val2))
You need the ‘car’ because you do ‘(list k v)’.
If you use ‘(cons k v)’, then you do not need the ‘car’.
[Q] Is there a way to using plists for return from find-my-marker-GOLD
and utilize in the user function test-GOLD
Yes, but probably you will want to avoid plists.