Hey, I just verified this piece about the commute function and I couldn't
believe my eyes!
(defn sleep-print-update
[sleep-time thread-name update-fn]
(fn [state]
(Thread/sleep sleep-time)
(println (str thread-name ": " state))
(update-fn state)))
(def counter (ref 0))
(future (dosync (commute counter (sleep-print-update 100 "Commute Thread A"
inc))))
(future (dosync (commute counter (sleep-print-update 150 "Commute Thread B"
inc))))
; printed output is:
Commute Thread A: 0 ; (after 100ms)
Commute Thread B: 0 ; (after 150ms)
Commute Thread A: 0 ; (after 200ms)
Commute Thread B: 1 ; (after 300ms)
So, when using commute the update function is ALWAYS run TWICE! Holey
Bovine, Batman!
What is the rationale for that? From all the other books I thought it only
re-tried if there was a conflict.
Alan
On Thu, Oct 22, 2015 at 4:32 PM, Daniel Higginbotham <[email protected]
> wrote:
> Clojure for the Brave and True <http://www.braveclojure.com/> is now
> available in print and ebook form <http://amzn.to/1UxrITn>, and the web
> site has been updated to incorporate all the changes that went into the
> print book. The web site remains 100% free :)
>
> In case you'd like to know more about the book: one of my goals was to
> explain concepts thoroughly and entertainingly.
>
> "Thoroughly" because of my own difficulties learning Clojure. For example,
> when I was first learning Clojure I thought the state constructs were very
> neat, but I didn't have the conceptual foundation to truly understand them
> and integrate them in my work. (I came from Ruby, where concurrency was too
> much of a hassle to bother with.) I didn't even have a clear idea of what a
> thread is. So, in the concurrency chapter, I try to explain what concurrent
> and parallel programming are and why they matter (and what threads are)
> before explaining atoms and refs and so forth.
>
> "Entertainingly" because otherwise it would be too tedious to complete the
> book. Plus, as a learner I think it's easier to stay focused and remember
> material if it makes you laugh. I'm really happy with the concurrency
> chapter in this regard too, where concurrency is explained in terms of the
> classic computer science example of Lady Gaga's song "Telephone," also
> known as the "I cannot text you with a drink in my hand, eh" problem.
>
> My other main goal was to provide the Clojure community with a free,
> high-quality online book for beginners. There are other great free
> resources available, like Clojure from the Ground Up (
> https://aphyr.com/tags/Clojure-from-the-ground-up), but I figure one more
> doesn't hurt. Clojure is one of the best things to happen to my career, and
> I hope my contribution helps the community :)
>
> Thanks!
> Daniel
>
>
> p.s. Thank you everyone who helped yesterday's launch go so well!
>
> --
> 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.
>
--
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.