On 17 December 2016 at 22:13, Rafo Ufoun <[email protected]> wrote:
> I'm a real newbe to clojure and functionnal programming but reading your
> answer raised a question :
>
> Where do you "store" the stopwatch object which you are passing to all
> your functions ?
>
> I understand that mutability is not needed to develop the functions you
> described but all of them take a stopwatch as as an argument. This
> stopwatch must be stored somewhere to call these functions against ?
>
Sure. At its simplest you could just store the stopwatch as a local
binding. For example, lets say we create an input loop:
(loop [stopwatch zeroed-stopwatch]
(wait-for-button)
(if (started? stopwatch)
(let [stopped-stopwatch (stop stopwatch)]
(println (elapsed stopped-stopwatch))
(recur stopped-stopwatch)
(recur (start stopwatch))))
The state of the stopwatch is stored as a loop variable. If we didn't want
an infinite loop, we could use a let form instead to similar effect.
If we have a more traditional GUI, then we have to communicate across
threads, and for that we'd use an atom (or possible a ref):
(let [stopwatch (atom zeroed-stopwatch]
(on-click button (fn [evt] (swap! stopwatch toggle))
(on-tick label (fn [evt] (set-text label (elapsed @stopwatch))))
Does that make things clearer?
- James
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.