[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: beginnerquestion (nconc)

From: John Mastro
Subject: Re: beginnerquestion (nconc)
Date: Tue, 21 Mar 2017 13:14:21 -0700

Stefan Huchler <address@hidden> wrote:
> Well to give you more context, most of the time I used doitems +
> push. and push works the "right" way. so no reversing is needed:
>   (dolist (item (append (let-alist kodi-properties .items) nil))
>     (push (list (spiderbit-get-id item)
>                 (vector `(,(spiderbit-get-name item)
>                           id ,(spiderbit-get-show-id item))))
>           tabulated-list-entries))
> I could revert here kodi-properties I guess to get in the end what I
> want?
> But now I needed a iterator in the alist assigend to the elements:
>   (let* ((items (cdr (assoc 'items kodi-properties))))
>     (dotimes (number (length items))
>       (let* ((item (elt items number))
>              (title (or (spiderbit-get-name item)
>                      "None"))
>              (id number))
>         (push (list `(,id)
>                     (vector `(,title id ,id)))
>               tabulated-list-entries))))
>   (setq tabulated-list-entries (nreverse tabulated-list-entries))
> So I would need to reverse here items and number or use something like
> length - number as index?
> it just dont feels very natural. I dont know.

It's not always a good candidate, but often you can use `mapcar' instead
of `push' and `nreverse'.

As an example (with the warning that I'm not familiar with kodi,
spiderbit, etc., so there may be problems with this):

(setq tabulated-list-entries
      (let ((id 0))
        (mapcar (lambda (item)
                  (let* ((title (or (spiderbit-get-name item) "None"))
                         (entry (list `(,id) (vector `(,title id ,id)))))
                    (setq id (1+ id)) ;; Or (cl-incf id)
                (cdr (assoc 'items kodi-properties)))))

The `push' plus `nreverse' idiom felt fairly unnatural to me too when I
first encountered Lisp. Before that, my main programming experience was
in Python, where the most natural way to work with lists (which are
dynamic arrays in Python's case) is to append at the end. Now, a few
years later, it feel natural enough, but perhaps that's Stockholm
syndrome talking :)


reply via email to

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