From 0c286f16862a05eb046b68f2521c1a3d97d56d78 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Thu, 31 Mar 2011 07:24:09 -0500 Subject: [PATCH] Provide example of coordinated ref update. --- src/koans/refs.clj | 19 ++++++++++++------- src/path_to_answer_sheet.clj | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/koans/refs.clj b/src/koans/refs.clj index 2a4a522..0754ff7 100644 --- a/src/koans/refs.clj +++ b/src/koans/refs.clj @@ -1,4 +1,5 @@ (def the-world (ref "hello")) +(def bizarro-world (ref {})) (meditations "In the beginning, there was a word" @@ -20,16 +21,20 @@ (alter the-world exclamator)) @the-world)) - "Though you should keep transactions as short as possible" - (= "better!!!!!!" (letfn [(exclamator [x] (str x "!"))] - (dosync - (alter the-world ___)) - @the-world)) - "Don't forget to do your work in a transaction!" (= 0 (do __ @the-world)) "Functions passed to alter may depend on the data in the ref" (= 20 (do - (dosync (alter the-world ___))))) + (dosync (alter the-world ___)))) + + "Two worlds are better than one" + (= ["Real Jerry" "Bizarro Jerry"] + (do + (dosync + (ref-set the-world {}) + (alter the-world assoc :jerry "Real Jerry") + (alter bizarro-world assoc :jerry "Bizarro Jerry") + __)))) + diff --git a/src/path_to_answer_sheet.clj b/src/path_to_answer_sheet.clj index 47db851..b46236c 100644 --- a/src/path_to_answer_sheet.clj +++ b/src/path_to_answer_sheet.clj @@ -140,9 +140,9 @@ "better" "better!!!" '(dosync (ref-set the-world 0)) + '(map :jerry [@the-world @bizarro-world]) ] - "___" ['(comp exclamator exclamator exclamator) - '(fn [x] (+ 20 x))]} + "___" ['(fn [x] (+ 20 x))]} "macros" {"__" ['~(first form) '~(nth form 2)