Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed zero_sum and cellular_automata labs

  • Loading branch information...
commit a41159188e0458a5168fc03d73f22414c1745331 1 parent 529ab69
Michael Kohl authored

Showing 2 changed files with 7 additions and 7 deletions. Show diff stats Hide diff stats

  1. +3 3 src/labs/cellular_automata.clj
  2. +4 4 src/labs/zero_sum.clj
6 src/labs/cellular_automata.clj
@@ -30,7 +30,7 @@
30 30 [:ol
31 31 [:li "Time to bring the GUI to life. Create " (c launch-2) ", which is the fleshed-out version of the trivial " (c launch-1) ". It should add lets for a " (c "BufferedImage img") " to do the drawing work offscreen, and for a " (c panel) " that proxies " (c JPanel) ". The proxy should implement " (c paint) " by calling a (yet-to-be-written) " (c render) " function."
32 32 (showme launch-2)]
33   - [:li "Render is going to iterate over the board, coloring cells white, gray, or black based on their on/dying/off status. Rather than have explicit loop variables, we are going to transform the board so that each cell knows its coordinates. Create a " (c with-coords) " function that takes a board and replace each cell with a " (c [status x y]) " tuple:"
  33 + [:li "Render is going to iterate over the board, coloring cells white, gray, or black based on their on/dying/off status. Rather than have explicit loop variables, we are going to transform the board so that each cell knows its coordinates. Create a " (c with-coords) " function that takes a board and replaces each cell with a " (c [status x y]) " tuple:"
34 34 (showme with-coords)]
35 35 [:li "Test " (c with-coords) " at the REPL:"
36 36 (repl-showme (with-coords (new-board [3 3])))]
@@ -52,7 +52,7 @@
52 52 []
53 53 [[:h3 "Let 'Er Rip"]
54 54 [:ol
55   - [:li "Each cell's state in the next step depends on the current state of its eight immediate neighbors. Clojure destructuring makes it easy to extract these neighbors from a list of lists. Write an " (c active-neighbors) " function that counts all the neighbors in the on state:"
  55 + [:li "Each cell's state in the next step depends on the current state of its eight immediate neighbors. Clojure destructuring makes it easy to extract these neighbors from a list of lists. Write an " (c active-neighbors) " function that counts all the neighbors in the " (c :on) " state:"
56 56 (showme active-neighbors)]
57 57 [:li "Test " (c active-neighbors) " against a board that is all on:"
58 58 (repl-showme (active-neighbors (repeat (repeat :on))))]
@@ -61,7 +61,7 @@
61 61 [:li "An " (c :on) " cell goes to " (c :dying)]
62 62 [:li "A " (c :dying) " cell goes to " (c :off)]
63 63 [:li "An " (c :off) " cell with two active (" (c :on) ") neighbors goes to " (c :on)]
64   - [:li "All other cells go to off"]]
  64 + [:li "All other cells go to " (c: off)]]
65 65 "Implement " (c brians-brain-rules) " as a function of three rows."
66 66 (showme brians-brain-rules)]
67 67 [:li "Test the rules at the REPL."
8 src/labs/zero_sum.clj
@@ -32,17 +32,17 @@
32 32 (showme accounts-1/total-balance)]
33 33 [:li "Create a " (c transfer) " function that takes a map keyed with " (c [:accounts :from :to :amount]) " and does a transfer. You will need to use " (c dosync) " to scope a transaction, and you should " [:i "not"] " need to actually read any balances."
34 34 (showme accounts-1/transfer)]
35   - [:li "Create a " (c balance) " function that takes " (c accounts) " and an " (c accound-id) " and returns the current balance for an account."
  35 + [:li "Create a " (c balance) " function that takes " (c accounts) " and an " (c account-id) " and returns the current balance for an account."
36 36 (showme accounts-1/balance)]
37 37 [:li "Using the REPL or a unit testing framework you already know to test these functions against the requirements listed at the start of the lab. Don't worry about the \"must be thread safe!\" requirement yet."]]])
38 38
39 39 (defn is-it-threadsafe
40 40 []
41   - [[:h3 "Is Is Threadsafe?"]
  41 + [[:h3 "Is It Threadsafe?"]
42 42 [:p "No."]
43 43 [:p "The transaction guarantees that all updates are atomic, consistent, and isolated. But the reads are not atomic, so " (c total-balance) " could be wrong. Let's prove it in code, by generating a bunch of random transactions on multiple threads and then watching reads get out of sync."]
44 44 [:ol
45   - [:li "First, we needs some random account ids: a " (c from-id) " and a " (c to-id) ". But in Clojure it rarely makes sense to deliver " [:i "two"] " of something, when you could deliver an lazy, infinite sequence instead. Write a " (c random-account-ids) " function that returns a lazy sequence of ids from accounts. You can use " (c clojure.contrib.seq-utils/rand-elts) " to get a random element from a collection, and you can use " (c repeatedly) " to return a lazy sequence of results from calling an impure function."
  45 + [:li "First, we need some random account ids: a " (c from-id) " and a " (c to-id) ". But in Clojure it rarely makes sense to deliver " [:i "two"] " of something, when you could deliver a lazy, infinite sequence instead. Write a " (c random-account-ids) " function that returns a lazy sequence of ids from accounts. You can use " (c clojure.contrib.seq-utils/rand-elts) " to get a random element from a collection, and you can use " (c repeatedly) " to return a lazy sequence of results from calling an impure function."
46 46 (showme accounts-1/random-account-ids)]
47 47 [:li "Now, create a " (c random-transfer) " function to do a transfer at random. Let " (c [from-id to-id]) " from " (c random-account-ids) ", then use " (c rand-int) " to let a random transfer amount, then make the " (c transfer) " :"
48 48 (showme accounts-1/random-transfer)]
@@ -64,7 +64,7 @@
64 64 [:li "A simple fix is to change " (c total-balance) " to read within a transaction, which guarantees that all reads are from the same point-in-time."
65 65 (showme accounts-2/total-balance)
66 66 "Now you should be able to run as long a " (c trial) " as you like without a problem."]]
67   - [:p "Reading from a transaction is fast, requires no locks, and never impedes writers. However, there is a way to solve this problem the avoids the read transaction, by changing the granularity we use to think about identity."]])
  67 + [:p "Reading from a transaction is fast, requires no locks, and never impedes writers. However, there is a way to solve this problem that avoids the read transaction, by changing the granularity we use to think about identity."]])
68 68
69 69 (defn granularity
70 70 []

0 comments on commit a411591

Please sign in to comment.
Something went wrong with that request. Please try again.