Callback interface

1 view
Skip to first unread message

Dustin Withers

unread,
Jun 22, 2008, 4:25:55 PM6/22/08
to Clojure
In Clojure and I guess other lisps what is the correct way to
implement a callback interface? Is there something like a delegate
function? Should I just be holding anonymous functions in a data
structure of some kind? I'm parsing serial streams and I need to call
functions depending on what's coming in.

-dustin

jim

unread,
Jun 22, 2008, 11:09:27 PM6/22/08
to Clojure
What I did was:

(def callback)
(defn dostuff [& args]
(do-some-stuff)
(callback msg)
(finish-up))

Then you can define 'callback' to be any function you want.

(defn callback [msg]
(println msg))

(all typed from memory, so take with a grain of salt)

The basic idea is to define a symbol that can then be assigned a
function later.

jim

Mike Hinchey

unread,
Jun 23, 2008, 10:37:45 AM6/23/08
to Clojure
Or the callback function can be specified when you make the call.
(defn dostuff [x callback] (callback x))
(dostuff 1 +)
(dostuff 1 -)

-Mike
Reply all
Reply to author
Forward
0 new messages