Heime <[email protected]> writes:
> This function returns keys from hast tables, plists, and alists.
> Should I include anything less or more to it?
>
> (defun aplt-keys (tpal)
> "Return keys from ALIST, PLIST, or HASH-TABLE.
> INPUT alist, plist, or hash-table"
>
> (cond
> ;; HASH TABLE
> ((hash-table-p tpal)
> (hash-table-keys tpal))
>
> ;; Property List (PLIST)
> ((and (listp tpal) (cl-evenp (length tpal)) (plistp tpal))
- The conditions ‘(listp tpal)’ and ‘(cl-evenp (length tpal))’
are redundant -- ‘(plistp tpal)’ will make the necessary
checks, so the ‘and’ can be reduced to a simple condition.
> (let (keys)
> (while tpal
> (push (car tpal) keys)
> (setq tpal (cddr tpal)))
- An equivalent alternative is:
(while plist
(push (pop plist) keys)
(pop plist))
> (nreverse keys)))
>
> ;; Association List (ALIST)
> ((and (listp tpal) (alistp tpal))
- The predicate ‘alistp’ does not exist, so you will need to
define it. It should make the condition ‘(listp tpal)’
redundant, as in the previous ‘cond’ clause.
> (mapcar #'car tpal))
>
> (t (message "Input Type not supported: %s" (type-of tpal)))))
>
- Consider changing this clause to return nil or signal an
error, that is, use (error "..." args), which will cause
Emacs to enter its debugger. Evaluate the following
expression to read the section in the Emacs Lisp reference
manual:
(info "(elisp) Signaling Errors")
- If you decide to return nil, then you will need to be able
to distinguish between a nil that means an invalid parameter
and a nil that means the list of keys is the empty list.
You might then decide to return a list of two values, the
(possibly empty) list of keys and a flag that indicates
whether an error was detected. Your function definition
would then include an expression such as:
(let ((list-of-keys nil)
(error-flag nil))
(cond
... ;; Clauses that set ‘list-of-keys’
(t (setq error-flag t)))
(list list-of-keys error-flag))
Code that calls your function could then check to see
whether ‘error-flag’, was set to ‘t’ (an error was
detected).
--
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.