Hi Laurent,
On Aug 5, 9:41 am, Laurent PETIT <[email protected]> wrote:
> Here is what I've come up in my head :
> * create a datastructure with 2 fields, one holding a future, one holding a
> delay. the future will "wait" for n millisecs, and then force the delay.
> * The datastructure will accept the future API and delegate all calls to
> the future,*except* for the deref call which will bypass the future and
> deref the delay instead
>
> What do you think ?
How about this:
(def *parser* (agent nil))
(defn schedule-parse
[]
(send *parser* (constantly (delay (parse-stuff)))))
(defn trigger-parse
([] (trigger-parse 0)
([delay-in-millis]
(future
(when (pos? delay-in-millis)
(Thread/sleep delay-in-millis))
(let [result (promise)]
(send *parser* #(do (deliver result @%) %)))
@result)))))
In the instantaneous case it creates an unnecessary future, but this
is the same with your approach as I understand it. One could save that
if one drops the requirement to implement the future API on the return
value. Or one wraps up the return value in a proxy implementing the
future API with no-ops + deref. The agent coordinates scheduled parser
runs. Changing the buffer schedules a new parse run. In between any
trigger-parse uses the cached one.
Does that make sense?
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en