This statements generates a list of (list filename buf), How could this be written to include an index as well?

eg: (list index filename buf) where the index would be a counter, starting at zero, increment 1 for each item.

  (lambda (buf) (list (buffer-file-name buf) buf))
  (seq-filter 'buffer-file-name (buffer-list))))
(let ((my-counter  0))
  (mapcar (lambda (buf) (list (cl-incf my-counter) (buffer-file-name buf) buf))
          (seq-filter 'buffer-file-name (buffer-list))))

Or if you want to use a global counter (e.g. for other purposes too):

(defvar my-counter 0 "My wonderful counter.")

(mapcar (lambda (buf) (list (cl-incf my-counter) (buffer-file-name buf) buf))
        (seq-filter 'buffer-file-name (buffer-list))))

If you don't want to use cl-incf then use (setq my-counter (1+ my-counter)).

  • Updated question to clarify what the index is. – ideasman42 Aug 17 '19 at 14:30
  • Can a locally defined variable be used (via let?) so it doesn't need to be global. – ideasman42 Aug 17 '19 at 14:38
  • Note, I though it might be possible to use a sequence generator in this case and merge the two sequences. – ideasman42 Aug 17 '19 at 14:39
  • What two sequences? But yes, lots of things are possible. – Drew Aug 17 '19 at 14:41
  • something like this: (merge-two-sequences (sequence of items) (generate-range)) – ideasman42 Aug 17 '19 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.