all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ted Zlatanov <tzz@lifelogs.com>
To: emacs-devel@gnu.org
Subject: Re: ELisp futures and continuations/coroutines
Date: Tue, 31 May 2011 05:53:17 -0500	[thread overview]
Message-ID: <87hb8bnntu.fsf@lifelogs.com> (raw)
In-Reply-To: 8762ozbkqp.fsf@lifelogs.com

[-- Attachment #1: Type: text/plain, Size: 924 bytes --]

On Tue, 24 May 2011 21:02:38 -0500 Ted Zlatanov <tzz@lifelogs.com> wrote: 

TZ> OK, I did as you asked.  See attached, which has commentary, error
TZ> handling, and ERT tests.  I rewrote some trivial functions as defmacros.

I'm trying to push and keep getting an error (break-lock didn't work):

Using saved push location: /home/tzz/source/emacs/trunk/
Unable to obtain lock  held by tzz                                                                                                                                         
at vcs-noshell [process #20860], acquired 1 second ago.
See "bzr help break-lock" for more.
bzr: ERROR: Could not acquire lock "(remote lock)": bzr+ssh://tzz@bzr.savannah.gnu.org/emacs/                                                                              

I have to run and can't figure out how to fix it; can someone please
commit the attached patch that adds url-future.el?

Thanks
Ted


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: url-future.patch --]
[-- Type: text/x-diff, Size: 5419 bytes --]

=== modified file 'lisp/url/ChangeLog'
--- lisp/url/ChangeLog	2011-05-29 02:45:03 +0000
+++ lisp/url/ChangeLog	2011-05-31 10:46:44 +0000
@@ -1,3 +1,7 @@
+2011-05-31  Teodor Zlatanov  <tzz@lifelogs.com>
+
+	* url-future.el: Add general futures facility.
+
 2011-05-29  Leo Liu  <sdl.web@gmail.com>
 
 	* url-cookie.el (url-cookie): Add option :named so that

=== added file 'lisp/url/url-future.el'
--- lisp/url/url-future.el	1970-01-01 00:00:00 +0000
+++ lisp/url/url-future.el	2011-05-31 10:45:57 +0000
@@ -0,0 +1,126 @@
+;;; url-future.el --- general futures facility for url.el
+
+;; Copyright (C) 2011  Free Software Foundation, Inc.
+
+;; Author: Teodor Zlatanov <tzz@lifelogs.com>
+;; Keywords: data
+
+;; This file is part of GNU Emacs.
+;;
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Make a url-future (basically a defstruct):
+;; (make-url-future :value (lambda () (calculation goes here))
+;;   :callback (lambda (future) (use future on success))
+;;   :errorback (lambda (future &rest error) (error handler)))
+
+;; Then either call it with `url-future-call' or cancel it with
+;; `url-future-cancel'.  Generally the functions will return the
+;; future itself, not the value it holds.  Also the functions will
+;; throw a url-future-already-done error if you try to call or cancel
+;; a future more than once.
+
+;; So, to get the value:
+;; (when (url-future-completed-p future) (url-future-value future))
+
+;; See the ERT tests and the code for futher details.
+
+;;; Code:
+
+(eval-when-compile (require 'cl))
+(eval-when-compile (require 'ert))
+
+(defstruct url-future callback errorback status value)
+
+(defmacro url-future-done-p (url-future)
+  `(url-future-status ,url-future))
+
+(defmacro url-future-completed-p (url-future)
+  `(eq (url-future-status ,url-future) t))
+
+(defmacro url-future-errored-p (url-future)
+  `(eq (url-future-status ,url-future) 'error))
+
+(defmacro url-future-cancelled-p (url-future)
+  `(eq (url-future-status ,url-future) 'cancel))
+
+(defun url-future-finish (url-future &optional status)
+  (if (url-future-done-p url-future)
+      (signal 'error 'url-future-already-done)
+    (setf (url-future-status url-future) (or status t))
+    ;; the status must be such that the future was completed
+    ;; to run the callback
+    (when (url-future-completed-p url-future)
+      (funcall (or (url-future-callback url-future) 'ignore)
+               url-future))
+    url-future))
+
+(defun url-future-errored (url-future errorcons)
+  (if (url-future-done-p url-future)
+      (signal 'error 'url-future-already-done)
+    (setf (url-future-status url-future) 'error)
+    (setf (url-future-value url-future) errorcons)
+    (funcall (or (url-future-errorback url-future) 'ignore)
+             url-future errorcons)))
+
+(defun url-future-call (url-future)
+  (if (url-future-done-p url-future)
+      (signal 'error 'url-future-already-done)
+    (let ((ff (url-future-value url-future)))
+      (when (functionp ff)
+        (condition-case catcher
+            (setf (url-future-value url-future)
+                  (funcall ff))
+          (error (url-future-errored url-future catcher)))
+        (url-future-value url-future)))
+    (if (url-future-errored-p url-future)
+        url-future
+      (url-future-finish url-future))))
+
+(defun url-future-cancel (url-future)
+  (if (url-future-done-p url-future)
+      (signal 'error 'url-future-already-done)
+    (url-future-finish url-future 'cancel)))
+
+(ert-deftest url-future-test ()
+  (let* ((text "running future")
+         (good (make-url-future :value (lambda () (format text))
+                                :callback (lambda (f) (set 'saver f))))
+         (bad (make-url-future :value (lambda () (/ 1 0))
+                               :errorback (lambda (&rest d) (set 'saver d))))
+         (tocancel (make-url-future :value (lambda () (/ 1 0))
+                                    :callback (lambda (f) (set 'saver f))
+                                    :errorback (lambda (&rest d)
+                                                 (set 'saver d))))
+         saver)
+    (should (equal good (url-future-call good)))
+    (should (equal good saver))
+    (should (equal text (url-future-value good)))
+    (should (url-future-completed-p good))
+    (should-error (url-future-call good))
+    (setq saver nil)
+    (should (equal bad (url-future-call bad)))
+    (should-error (url-future-call bad))
+    (should (equal saver (list bad '(arith-error))))
+    (should (url-future-errored-p bad))
+    (setq saver nil)
+    (should (equal (url-future-cancel tocancel) tocancel))
+    (should-error (url-future-call tocancel))
+    (should (null saver))
+    (should (url-future-cancelled-p tocancel))))
+
+(provide 'url-future)
+;;; url-future.el ends here


  reply	other threads:[~2011-05-31 10:53 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-14 10:32 more url-utils? joakim
2011-05-14 11:21 ` Lars Magne Ingebrigtsen
2011-05-14 11:42   ` joakim
2011-05-15  1:59     ` Lars Magne Ingebrigtsen
2011-05-14 20:42   ` Dimitri Fontaine
2011-05-15 14:26   ` Jason Rumney
2011-05-16 18:25     ` Ted Zlatanov
2011-05-16 19:03       ` Stefan Monnier
2011-05-16 19:17         ` Ted Zlatanov
2011-05-16 19:22           ` Lennart Borgman
2011-05-16 19:32             ` Ted Zlatanov
2011-05-16 19:33               ` Lennart Borgman
2011-05-16 19:55                 ` Ted Zlatanov
2011-05-16 19:52               ` Julien Danjou
2011-05-16 20:18                 ` Ted Zlatanov
2011-05-17  3:04                   ` Stefan Monnier
2011-05-17  8:13                   ` Julien Danjou
2011-05-16 19:53           ` Stefan Monnier
2011-05-16 21:07             ` joakim
2011-05-16 21:18               ` Ted Zlatanov
2011-05-16 21:21                 ` Lennart Borgman
2011-05-16 21:58                 ` joakim
2011-05-17 11:40             ` Lars Magne Ingebrigtsen
2011-05-17 13:34               ` Stefan Monnier
2011-05-14 13:21 ` Ted Zlatanov
2011-05-16 13:15   ` Stefan Monnier
2011-05-16 15:19     ` Tom Tromey
2011-05-16 15:45       ` Stefan Monnier
2011-05-16 17:04       ` joakim
2011-05-16 17:17         ` Lennart Borgman
2011-05-16 17:30         ` Stefan Monnier
2011-05-16 18:39         ` Julien Danjou
2011-05-15 13:34 ` Chong Yidong
2011-05-17 22:26   ` Ted Zlatanov
2011-05-18  0:11     ` Stefan Monnier
2011-05-18  2:14       ` Ted Zlatanov
2011-05-18 12:16         ` Stefan Monnier
2011-05-18 13:44           ` Ted Zlatanov
2011-05-18 14:09             ` Stefan Monnier
2011-05-18 14:43               ` Ted Zlatanov
2011-05-18 22:05                 ` Stefan Monnier
2011-05-19  1:57                   ` Ted Zlatanov
2011-05-19  2:28                     ` Stefan Monnier
2011-05-19 10:28                       ` Ted Zlatanov
2011-05-19 11:53                         ` Stefan Monnier
2011-05-19 12:43                           ` Ted Zlatanov
2011-05-19 13:28                             ` Stefan Monnier
2011-05-19 13:35                               ` Ted Zlatanov
2011-05-30 17:31                               ` Lars Magne Ingebrigtsen
2011-05-30 19:09                                 ` Stefan Monnier
2011-05-30 19:38                                   ` Lars Magne Ingebrigtsen
2011-05-30 22:17                                     ` Stefan Monnier
2011-05-30 23:11                                       ` Lars Magne Ingebrigtsen
2011-05-31  1:26                                         ` Stefan Monnier
2011-05-31 10:15                                           ` Lars Magne Ingebrigtsen
2011-05-31 12:45                                             ` Stefan Monnier
2011-05-31 18:38                                               ` Lars Magne Ingebrigtsen
2011-05-19 14:11                             ` joakim
2011-05-19 14:40                               ` Stefan Monnier
2011-05-19 13:46                   ` ELisp futures and continuations/coroutines (was: more url-utils?) Ted Zlatanov
2011-05-19 14:15                     ` ELisp futures and continuations/coroutines Stefan Monnier
2011-05-19 14:16                     ` joakim
2011-05-19 15:24                       ` Ted Zlatanov
2011-05-19 15:09                     ` Thien-Thi Nguyen
2011-05-19 15:31                       ` Ted Zlatanov
2011-05-19 16:40                         ` Thien-Thi Nguyen
2011-05-19 23:48                           ` Ted Zlatanov
2011-05-20  1:23                             ` Thien-Thi Nguyen
2011-05-20  4:18                               ` Mohsen BANAN
2011-05-20 15:30                                 ` Ted Zlatanov
2011-05-20 15:29                               ` Ted Zlatanov
2011-05-22 13:17                                 ` Thien-Thi Nguyen
2011-05-23 14:24                                   ` Ted Zlatanov
2011-05-23 14:59                                     ` Stefan Monnier
2011-05-23 15:10                                       ` Ted Zlatanov
2011-05-23 15:42                                         ` SAKURAI Masashi
2011-05-25  2:07                                           ` Ted Zlatanov
2011-05-26  0:23                                             ` SAKURAI Masashi
2011-06-03 13:59                                             ` SAKURAI Masashi
2011-06-03 16:06                                               ` Ted Zlatanov
2011-06-04  6:21                                                 ` SAKURAI Masashi
2011-06-29  5:31                                                 ` SAKURAI Masashi
2011-06-29 12:01                                                   ` Ted Zlatanov
2011-06-29 17:33                                                     ` SAKURAI Masashi
2011-06-29 16:09                                                   ` Chong Yidong
2011-05-23 15:45                                         ` Stefan Monnier
2011-05-25  2:02                                           ` Ted Zlatanov
2011-05-31 10:53                                             ` Ted Zlatanov [this message]
2011-05-31 16:55                                               ` Ted Zlatanov
2011-05-19 17:03                     ` ELisp futures and continuations/coroutines (was: more url-utils?) SAKURAI Masashi
2011-05-19 22:51                       ` ELisp futures and continuations/coroutines Ted Zlatanov
2011-05-20 15:49                         ` SAKURAI Masashi
2011-05-25  2:17                           ` Ted Zlatanov

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

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

  git send-email \
    --in-reply-to=87hb8bnntu.fsf@lifelogs.com \
    --to=tzz@lifelogs.com \
    --cc=emacs-devel@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.