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

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

Re: problems of everyday life


From: Akib Azmain Turja
Subject: Re: problems of everyday life
Date: Wed, 16 Nov 2022 18:10:54 +0600

Juri Linkov <juri@linkov.net> writes:

>> or maybe
>>
>>   (apply #'+ '(5 7 10))
>>
>> or ... well, what do you prefer?
>
> This reminds the implementation of sleep sort:
>
>   (dolist (i '(3 1 4 1 5 92 65 3 5 89 79 3))
>     (run-with-timer (* i 0.001) nil 'message "%d" i))
>
> The sorted output printed in the *Messages* buffer:
>
>   1 [2 times]
>   3 [3 times]
>   4
>   5 [2 times]
>   65
>   79
>   89
>   92
>

Someone implemented addition using sleep.  Now I have implemented
subtraction:

--8<---------------cut here---------------start------------->8---
(defun subtract (x y)
  (when (< y x)
    (let ((tmp x))
      (setq x y)
      (setq y tmp)))
  (let ((time-a (current-time)))
    (sleep-for x)
    (let ((time-b (current-time)))
      (while (< (float-time (time-subtract nil time-a)) y)
        (sleep-for 0.9))
      (floor (float-time (time-subtract nil time-b))))))

(subtract 0 1)     ; => 1
(subtract 3 5)     ; => 2
--8<---------------cut here---------------end--------------->8---

-- 
Akib Azmain Turja, GPG key: 70018CE5819F17A3BBA666AFE74F0EFA922AE7F5
Fediverse: akib@hostux.social
Codeberg: akib
emailselfdefense.fsf.org | "Nothing can be secure without encryption."

Attachment: signature.asc
Description: PGP signature


reply via email to

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