I'm still getting used to multiple return values. What's the easiest
way to make a list that contains month and year from a universal-time?
I can do this, but it seems horribly verbose for what it does:
(multiple-value-bind
(second minute hour date month year day daylight-p zone)
(decode-universal-time timestamp)
(list month year))
Ari.
--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
|
|
0
|
|
|
|
Reply
|
ari (61)
|
8/21/2005 6:27:46 AM |
|
K. Ari Krupnikov wrote:
> I'm still getting used to multiple return values. What's the easiest
> way to make a list that contains month and year from a universal-time?
>
> I can do this, but it seems horribly verbose for what it does:
>
> (multiple-value-bind
> (second minute hour date month year day daylight-p zone)
> (decode-universal-time timestamp)
> (list month year))
>
> Ari.
>
Hmm. I got:
(defun nths (indices list)
(loop for i in indices
collecting (nth i list)))
(nths '(4 5) (multiple-value-list (decode-universal-time timestamp)))
Perhaps it might be worth making a function like
(get-time-fields (:month :year) (decode-universal-time timestamp))
If you'll be splitting apart times a lot.
|
|
0
|
|
|
|
Reply
|
nathan_baum (228)
|
8/21/2005 6:49:27 AM
|
|
Hi,
Nathan Baum <nathan_baum@btinternet.com> writes:
> K. Ari Krupnikov wrote:
>> I'm still getting used to multiple return values. What's the easiest
>> way to make a list that contains month and year from a universal-time?
>> I can do this, but it seems horribly verbose for what it does:
>> (multiple-value-bind
>> (second minute hour date month year day daylight-p zone)
>> (decode-universal-time timestamp)
>> (list month year))
>> Ari.
>
> Hmm. I got:
>
> (defun nths (indices list)
> (loop for i in indices
> collecting (nth i list)))
>
> (nths '(4 5) (multiple-value-list (decode-universal-time timestamp)))
While that solution has the advantage to work at runtime, macrology
helps even more if one does not need that flexibility. For example,
(binding-some-values ((4 month) (5 year))
(decode-universal-time timestamp)
(list month year))
can be transformed using ('cond-let' is described below):
(defmacro binding-some-values (some-bindings form &body body)
(labels ((collect-bindings (index bindings ignored)
(cond-let (_ (minusp index)
(values bindings ignored))
(entry (assoc index some-bindings)
(collect-bindings (1- index)
(cons (second entry) bindings)
ignored))
(_ t
(let ((symbol (gensym "IGNORED-VALUE-")))
(collect-bindings (1- index)
(cons symbol bindings)
(cons symbol ignored)))))))
(multiple-value-bind (bindings ignored)
(collect-bindings (reduce #'max some-bindings :key #'car) nil nil)
`(multiple-value-bind ,bindings
,form
(declare (ignore ,@ignored))
,@body))))
Maybe using positional parameters is more intuitive:
(multiple-value-bind*
(_ _ _ _ month year)
(decode-universal-time 0)
(list month year))
Such a form is transformable by:
(defmacro multiple-value-bind* (maybe-vars form &body body)
(let ((pairs (mapcar (lambda (symbol)
(if (eq symbol '_)
(let ((ignored (gensym "IGNORED-VALUE-")))
(cons ignored ignored))
(cons symbol nil))) maybe-vars)))
`(multiple-value-bind
,(mapcar #'car pairs)
,form
(declare (ignore ,@(remove nil (mapcar #'cdr pairs))))
,@body)))
Here is the 'cond-let' utility used in 'binding-some-values':
(defmacro cond-let (&rest clauses)
"Syntax: COND-LET {no-binding-clause | binding-clause}* => result*
no-binding-clause ::= (_ test-form form*)
binding-clause ::= (var test-form form*)
COND-LET is similar to COND, but allows binding the result of the
test forms. The expansion of 'clause' depends on its first
element:
* _: No binding is established, 'test-form' and 'form*' are used
exactly like in COND;
* Other symbol: a new binding is established between that symbol
and the result of evaluating 'test-form'; the 'form*' forms
are executed using that binding when its value is not NIL."
(when clauses
(destructuring-bind (clause . other-clauses) clauses
(destructuring-bind (binding test-form &body body) clause
(flet ((gen-1 (test)
`(if ,test (progn ,@body) (cond-let ,@other-clauses))))
(case binding
(_
(gen-1 test-form))
(otherwise
`(let ((,binding ,test-form))
,(gen-1 binding)))))))))
Cu,
Damien.
--
http://foobox.net/~dash/
I can resist everything except temptation.
--Oscar Wilde
|
|
0
|
|
|
|
Reply
|
dash1 (14)
|
8/21/2005 12:59:54 PM
|
|
> I can do this, but it seems horribly verbose for what it does:
>
> (multiple-value-bind
> (second minute hour date month year day daylight-p zone)
> (decode-universal-time timestamp)
> (list month year))
This is slightly less verbose:
(multiple-value-bind (s m h d mn yr)
(decode-universal-time timestamp)
(declare (ignorable (s m h d)))
(list (mn yr)))
You don't need to bind to any variables beyond the last one you're
interested in.
-jeff cunningham
|
|
0
|
|
|
|
Reply
|
jeffrey4 (78)
|
8/22/2005 3:07:16 AM
|
|
Jeff Cunningham <jeffrey@cunningham.net> writes:
> You don't need to bind to any variables beyond the last one you're
> interested in.
Ah! Thank you. I missed that fact.
Still verbose, but a little shorter.
Ari.
--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
|
|
0
|
|
|
|
Reply
|
ari (61)
|
8/22/2005 6:00:30 AM
|
|
|
4 Replies
26 Views
(page loaded in 0.127 seconds)
Similiar Articles: Leap Second - comp.protocols.time.ntpThe leap is announced at the beginning of the month to ... century or two, when there are more than 2 per year ... that is occasionally applied to Coordinated Universal Time (UTC ... Can or should the NTP protocol eventually serve timezone data ...(VLF time transmitters really don't do so either, so ... own update mechanism, and there should not be a universal ... Download it once a month and install it via a cron job ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... Where did Fortran go? - comp.lang.fortranMay be time has really changed since I played ... Just a few months ago, maybe a year, a mutual coleague of a friend ... which will potentially be of fairly universal ... X10 PC Interface upgrade - comp.home.automation... new flash file to fix the time change .... so you do it manually twice a year. ... m doing it with a few >> X10 universal ... are still in boxes even after 8 months ... How to check whether malloc has allocated memory properly in case ...After >all, what I most recall as near universal ... programmer might take several months or years to ... of days or weeks, and used for a few months, at which time ... RESIGNATION OF STEVE JOBS!! - comp.sys.mac.system... years ahead of Windows and MacOS of the time. But that was many years ... But remember how, only short months ago, when ... Go to Apple's website and look up 'Universal Binary'!!!! Haas mill advice - comp.cad.solidworks... proportional to reliability >> over time. In my rather limited 30 years of ... you will grow out of the VF-2 in 6-12 months ... that most machines get a sub- > plate / universal ... top 10 uses for random data compression?? anyone? - comp ...bastions kept her name in the papers for six months. ... Big-time literary mogul Scott Meredith was her agent. ... The stands, petrols, and writings are all universal and ... Year 2012 Calendar – United States - timeanddate.comCalendar Generator – Create a calendar for any year; Monthly calendar – shows only month at a time; Custom calendar – make advanced customized calendars Months to Years Conversion Calculator - Unit Conversion - Online ...Use the following calculator to convert between months and years. If you need to convert months to other units, please try our universal Time Unit Converter. 7/26/2012 9:36:14 PM
|