Very clear thank you !
I can't wait to know these concept as I am discovering clojure with the
'Functionnal programming for OO programmer' book. Thank you again
Le samedi 17 décembre 2016 18:53:31 UTC-5, James Reeves a écrit :
>
> On 17 December 2016 at 22:13, Rafo Ufoun
On 17 December 2016 at 22:13, Rafo Ufoun 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
On Dec 10, 2016 1:47 AM, "Didier" wrote:
I'm wondering what everyone thinks of using closures to mimic a simplistic
object system in Clojure? I'm not sure what to think of it yet, but the
idea is that you wrap object fields inside a closed function, and it
returns a map of
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
Let me add a couple of comments, both from a clojure perspective and from
other languages.
First, I have used this pattern in clojure and similar languages. I find it
is often useful when I need to deal with multiple functions mutating the
same state. In clojure, I used this to make little
Logan's point about being able to add new functions on a stopwatch is
valid. That's often the argument to why protocols are better then classic
OO. Though I still feel like in some scenarios, I think this is a good
pattern, and can serve us better, the stopwatch being one good example.
Nothing
You don't need mutability to represent a stopwatch.
(defn start [stopwatch]
(assoc stopwatch ::start-time (System/currentTimeMillis)))
(defn elapsed-since-started [stopwatch]
(- (System/currentTimeMillis) (::start-time stopwatch)))
(defn stop [stopwatch]
(-> stopwatch
I feel like you would be better off separating functions from the data they
operate on here. In this case, you could represent the state of a stopwatch
with a map containing the start time and the time elapsed, and have
functions `stop`, `reset`, `start`, etc that take the stopwatch data
I'm wondering what everyone thinks of using closures to mimic a simplistic
object system in Clojure? I'm not sure what to think of it yet, but the
idea is that you wrap object fields inside a closed function, and it
returns a map of methods that operates over those fields.
Here's an example of