help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: using setq to create lists based on other lists...


From: Stefan Monnier
Subject: Re: using setq to create lists based on other lists...
Date: Sun, 02 Dec 2018 12:02:50 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

> I see. Eventually I used copy-tree instead to initialize the new lists and
> then modified them separately with setcar.

So you're back to using `setcar` :-(

> Just out of curiosity, let me post what I did. There are probably
> better ways to do it, but that's the best I could come up with
> today. I'd love to be able to think in terms closer to what elisp
> allows though.

Those date/time thingies are indeed rather annoying to construct.
copy-sequence + in-place modification is probably the best you can
use, indeed :-(

>   (setq now (decode-time (float-time))
>       myDateLastMonth (copy-tree now)
>       myDateThisMonth (copy-tree now)
>       myDateNextMonth (copy-tree now)
>       now (encode-time now 'integer))

You're using `setq` on vars you haven't declared/defined yet!
And theses aren't "trees" but lists/sequences, so better use
`copy-sequence` which is also more efficient:

    (let* ((decoded-now (decode-time (float-time)))
           (myDateLastMonth (copy-tree now))
           (myDateThisMonth (copy-tree now))
           (myDateNextMonth (copy-tree now))
           (encoded-now (encode-time now 'integer)))

I recommend you put `-*- lexical-binding:t -*-` somewhere on the first
line of your file, and that you `M-x byte-compile RET` your file so
Emacs can help you catch some of those issues.

>   ;; create the data for last month
>   (setcar (cdr (cdr (cdr myDateLastMonth))) myDay)

Better write this as (setf (nth 3 myDateLastMonth) myDay)
It's bad enough that the fields aren't named so you have to refer to
them by position, but having to count `cdr`s is really annoying IMO.


-- Stefan




reply via email to

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