unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jean-Christophe Helary <brandelune@gmail.com>
To: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: using setq to create lists based on other lists...
Date: Mon, 3 Dec 2018 01:23:25 +0900	[thread overview]
Message-ID: <6641A86F-1E33-44BA-8CEE-625F3213D9C4@gmail.com> (raw)
In-Reply-To: <jwvr2ezj4ur.fsf-monnier+gmane.emacs.help@gnu.org>



> On Dec 3, 2018, at 1:05, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> 
>> When I check "Modifying Existing List Structure" the only things I find of
>> any use in my case are setcar/setcdr.
> 
> That's because "modifying" is the wrong way to think about it.
> 
> That section should start by telling the user that most likely what they
> really want to do is something else (i.e. get a similar list structure
> with some differences).

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

>> I would certainly have considered that if I had known that setq was
>> linking the lists :)
> 
> Clearly, "linking the lists" means something to you, but it means
> nothing to me, so the manual can't really say something like that.

I think somebody used that term in the thread. I didn't even have that concept before the discussion started :)

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.

Jean-Christophe Helary
-----------------------------------------------
http://mac4translators.blogspot.com @brandelune


;; The function takes a date d and returns a (d m y) list where (d m y) is the closest day to today
;; For ex, today is 2018/12/3
;; d=1 -> (1 12 2018)
;; d=30 -> (30 11 2018)
;; d=15 -> (15 12 2018)
;; I use the function in a template for a blog that I write one page a day.
;; When I launch the template for a given day, it creates links for the previous day and the next day
;; I used the "closest" assumption because I'm either writing the blog late, or outputing future days templates for preparation.


(defun getMyDate (myDay)
  (interactive)
  (if (or (> 1 myDay) (< 32 myDay))
      (setq myDay (nth 3 (decode-time))))
  (setq now (decode-time (float-time))
	myDateLastMonth (copy-tree now)
	myDateThisMonth (copy-tree now)
	myDateNextMonth (copy-tree now)
	now (encode-time now 'integer))

  ;; need to create "last month", "next month", "last year", "next year"
  ;; 1 day is 84600 seconds
  ;; 1 month is approximately 2,592,000 seconds
  ;; 1 year is approximately 31,536,000 seconds
  (setq yesterDay (nth 3 (decode-time (- (float-time) 84600))) ;; not used in the function
	toMorrow (nth 3 (decode-time (+ (float-time) 84600))) ;; not used in the function
	lastMonth (nth 4 (decode-time (- (float-time) 2592000)))
	thisMonth (nth 4 (decode-time)) ;; not used in the function
	nextMonth (nth 4 (decode-time (+ (float-time) 2592000)))
	lastYear (nth 5 (decode-time (- (float-time) 31536000)))
	thisYear (nth 5 (decode-time)) ;; not used in the function
	nextYear (nth 5 (decode-time (+ (float-time) 31536000))))

  ;; create the data for last month
  (setcar (cdr (cdr (cdr myDateLastMonth))) myDay)
  (setcar (cdr (cdr (cdr (cdr myDateLastMonth)))) lastMonth)
  (if (= lastMonth 12)
      (setcar (cdr (cdr (cdr (cdr (cdr myDateLastMonth))))) lastYear))

  ;; create the data for this month
  (setcar (cdr (cdr (cdr myDateThisMonth))) myDay)

  ;; create the data for next month
  (setcar (cdr (cdr (cdr myDateNextMonth))) myDay)
  (setcar (cdr (cdr (cdr (cdr myDateNextMonth)))) nextMonth)
  (if (= nextMonth 1)
      (setcar (cdr (cdr (cdr (cdr (cdr myDateNextMonth))))) nextYear))

  ;; compare the dates to find the closest
  (setq a (abs (- now (encode-time myDateLastMonth 'integer)))
	b (abs (- now (encode-time myDateThisMonth 'integer)))
	c (abs (- (encode-time myDateNextMonth 'integer))))

  (if (and (< a b) (< b c))
      (setq myMonth (nth 4 myDateLastMonth)
	    myYear (nth 5 myDateLastMonth))
    (if (and (< b a) (< b c))
	(setq myMonth (nth 4 myDateThisMonth)
	      myYear (nth 5 myDateThisMonth))
      (setq myMonth (nth 4 myDateNextMonth)
	    myYear (nth 5 myDateNextMonth))))
  ;; et voilà
  (list myDay myMonth myYear))

(getMyDate 30)



  reply	other threads:[~2018-12-02 16:23 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-02 10:53 using setq to create lists based on other lists Jean-Christophe Helary
2018-12-02 15:07 ` Stefan Monnier
2018-12-02 15:41   ` Jean-Christophe Helary
2018-12-02 16:05     ` Stefan Monnier
2018-12-02 16:23       ` Jean-Christophe Helary [this message]
2018-12-02 17:02         ` Stefan Monnier
2018-12-02 17:21           ` Jean-Christophe Helary
2018-12-02 19:11             ` Robert Thorpe
2018-12-02 23:44               ` Jean-Christophe Helary
     [not found]   ` <mailman.5028.1543765273.1284.help-gnu-emacs@gnu.org>
2018-12-03 13:43     ` Rusi
     [not found] <mailman.5010.1543748027.1284.help-gnu-emacs@gnu.org>
2018-12-02 11:21 ` Barry Margolin
2018-12-02 11:51   ` Stephen Berman
2018-12-02 12:22     ` Jean-Christophe Helary
2018-12-02 13:08       ` Stephen Berman
2018-12-02 13:28         ` Jean-Christophe Helary
2018-12-02 14:40           ` Michael Heerdegen
2018-12-02 15:34             ` Jean-Christophe Helary
2018-12-02 15:44               ` Michael Heerdegen
2018-12-02 15:57                 ` Jean-Christophe Helary
2018-12-02 15:00           ` Stephen Berman
2018-12-02 15:30             ` Jean-Christophe Helary
     [not found]             ` <mailman.5026.1543764670.1284.help-gnu-emacs@gnu.org>
2018-12-04  9:00               ` Barry Margolin
2018-12-02 12:03   ` Jean-Christophe Helary
     [not found] <mailman.5042.1543777897.1284.help-gnu-emacs@gnu.org>
2018-12-04  9:04 ` Barry Margolin
     [not found]   ` <(message>
     [not found]     ` <from>
     [not found]       ` <Barry>
     [not found]         ` <Margolin>
     [not found]           ` <on>
     [not found]             ` <Tue>
     [not found]               ` <04>
     [not found]                 ` <Dec>
     [not found]                   ` <2018>
     [not found]                     ` <04:04:52>
2018-12-04 13:56   ` Stefan Monnier
2018-12-05  1:07   ` Robert Thorpe
2018-12-05  2:32     ` Drew Adams
2018-12-05  6:45       ` Jean-Christophe Helary
2018-12-05  8:00         ` Marcin Borkowski
2018-12-05  8:11           ` Jean-Christophe Helary
2018-12-05 14:57         ` Drew Adams
     [not found]         ` <mailman.5218.1544021892.1284.help-gnu-emacs@gnu.org>
2018-12-05 16:59           ` Barry Margolin
     [not found]     ` <mailman.5186.1543978155.1284.help-gnu-emacs@gnu.org>
2018-12-05 16:50       ` Barry Margolin
     [not found]   ` <mailman.5145.1543931778.1284.help-gnu-emacs@gnu.org>
2018-12-05 16:47     ` Barry Margolin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6641A86F-1E33-44BA-8CEE-625F3213D9C4@gmail.com \
    --to=brandelune@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).