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

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

Re: Making sure I'm not checking email (or doing other things) too often


From: Marcin Borkowski
Subject: Re: Making sure I'm not checking email (or doing other things) too often
Date: Sun, 06 Mar 2016 14:36:17 +0100
User-agent: mu4e 0.9.13; emacs 25.1.50.6

On 2016-03-06, at 14:26, Marcin Borkowski <mbork@mbork.pl> wrote:

> On 2016-02-15, at 15:40, Michael Heerdegen <michael_heerdegen@web.de> wrote:
>
>> Factoring out the greatest common divider would give you something like
>> this:
>>
>> --8<---------------cut here---------------start------------->8---
>> -*- lexical-binding: t -*-
>>
>> (defun stopwatch ()
>>   "Return a fresh stopwatch.
>> This is a function accepting zero arguments that upon each call
>> will return the time difference from its last call in seconds.
>> When called the first time it will return nil."
>>   (let ((last-invocation nil))
>>     (lambda ()
>>       (prog1 (and last-invocation
>>                    (time-to-seconds (time-subtract (current-time) 
>> last-invocation)))
>>          (setq last-invocation (current-time))))))
>> --8<---------------cut-here---------------end--------------->8---
>>
>> With that, one could make your advice functions reference and use such a
>> stopwatch object (each an individual one, of course).
>
> Thanks.  This is my version:
>
> (defun create-stopwatch ()
>   "Return a closure which returns time (in seconds) since its
> last invocation every time it is called."
>   (let (last-invocation-time (current-time))
>     (lambda ()
>       (prog1
>         (time-to-seconds (time-subtract (current-time) last-invocation-time))
>       (setq last-invocation-time (current-time))))))
>
> I don't claim it to be better, but I noticed a strange phenomenon.  When
> I invoke the "stopwatch object" (closure, in fact) for the first time,
> I get negative result.  Why is that so?

Never mind, I'm an idiot.

BTW: could the byte compiler warn about using

(let (var)
  ...)

instead of
(let ((var))
  ...)

?

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



reply via email to

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