Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16928
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | "Pascal J. Bourguignon" <pjb@informatimago.com> |
| Newsgroups | comp.lang.c, comp.lang.c++, comp.unix.programmer, comp.lang.javascript, comp.lang.lisp |
| Subject | Re: avcall, callback, trampoline, vacall, etc |
| Date | Sun, 28 Oct 2012 18:56:07 +0100 |
| Organization | Informatimago |
| Lines | 69 |
| Message-ID | <87r4oiiiq0.fsf@informatimago.com> (permalink) |
| References | <dfa26195-ed53-41e7-afb6-6f59d556a154@3g2000yqn.googlegroups.com> <1ba9va6kmw.fsf@new-snowball.wb.pfeifferfamily.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| X-Trace | individual.net pQCxIIhV8eAfEMry/FkSuAwv2EnRwCqYurS9+wf5BXn36EcIaC+aiqqLvN0PcanPgF |
| Cancel-Lock | sha1:N2RmMDMwMjJkYzk1MWFmMmU0MTI0YmNjNjA4OWE0YTkzZDRlNjllMQ== sha1:dRrZeAL+LpZsgWni1nfhqcTBL2U= |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== |
| X-Accept-Language | fr, es, en |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.4 (darwin) |
| Xref | csiph.com comp.lang.c:27799 comp.lang.c++:19315 comp.unix.programmer:3680 comp.lang.javascript:16928 comp.lang.lisp:11798 |
Cross-posted to 5 groups.
Show key headers only | View raw
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> For callback-based programming, forget all that. Flush it.
>
> There are areas where a completely different paradigm is used. A good example
> of one of these areas is writing a GUI interface. The big thing about a
> GUI interface is that you don't have control of the order in which input
> arrives, and you need to do something about whatever the crazy user
> does. In this world, somebody else writes something called an "event
> loop". The event loop just sits out there waiting for input (maybe a
> button click, maybe information that you need to redraw your window,
> maybe a timeout, maybe something else), and when the input happens the
> event loop code calls a function you wrote to handle that particular
> input. Your function is called a 'callback'.
Yes, but in general, there is still this direct and simple flow of
control: a function calls another function, and so on.
A callback is just a function that is given to some module, to be called
when the module needs to call it. This is a way to parameterize or
customize that module.
(defun my-callback (data)
(format t "Received ~A~%" data))
(defun receive-loop (data-callback)
(loop
:for line = (progn (format *query-io* "> ")
(finish-output *query-io*)
(read-line *query-io* nil nil))
:while (and line (plusp (length line)))
:do (funcall data-callback line)))
cl-user> (receive-loop 'my-callback)
> Hello
Received Hello
> World
Received World
>
nil
cl-user>
And then you can use the receive-loop module with a different callback,
making it do something else:
cl-user> (let ((c 0))
(receive-loop (lambda (data) (incf c (parse-integer data))))
c)
> 42
> 33
> 101
>
176
cl-user>
Now, there's another situation where you really don't know WHEN the
callback is called, it's when the module calling it uses threads, and
calls your callback function at "random" times, possibly in another
thread, or perhaps while handling a unix signal. In this situation,
there are a lot of technicalities to fulfill for calbacks.
But the in usual case, there's no complexity involved.
--
__Pascal Bourguignon__
http://www.informatimago.com
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
avcall, callback, trampoline, vacall, etc gnuist007@hotmail.com - 2012-10-25 12:24 -0700
Re: avcall, callback, trampoline, vacall, etc James Kuyper <jameskuyper@verizon.net> - 2012-10-25 16:04 -0400
Re: avcall, callback, trampoline, vacall, etc Gene Wirchenko <genew@ocis.net> - 2012-10-25 14:55 -0700
Re: avcall, callback, trampoline, vacall, etc Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-25 15:46 -0500
Re: avcall, callback, trampoline, vacall, etc Cortez <relativeflux@hotmail.co.uk> - 2012-10-25 13:47 -0700
Re: avcall, callback, trampoline, vacall, etc Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-25 20:18 -0600
Re: avcall, callback, trampoline, vacall, etc gnuist007@hotmail.com - 2012-10-26 10:11 -0700
Re: avcall, callback, trampoline, vacall, etc "Pascal J. Bourguignon" <pjb@informatimago.com> - 2012-10-28 18:56 +0100
Re: avcall, callback, trampoline, vacall, etc Rivka Miller <rivkaumiller@gmail.com> - 2012-11-05 16:28 -0800
Re: avcall, callback, trampoline, vacall, etc Les Cargill <lcargill99@comcast.com> - 2012-10-26 07:27 -0500
Re: avcall, callback, trampoline, vacall, etc gnuist007@hotmail.com - 2012-10-26 10:16 -0700
Re: avcall, callback, trampoline, vacall, etc Les Cargill <lcargill99@comcast.com> - 2012-10-26 12:42 -0500
Re: avcall, callback, trampoline, vacall, etc James Kuyper <jameskuyper@verizon.net> - 2012-10-26 14:08 -0400
csiph-web