Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed zero_sum and cellular_automata labs

  • Loading branch information...
commit a41159188e0458a5168fc03d73f22414c1745331 1 parent 529ab69
Michael Kohl authored
Showing with 7 additions and 7 deletions.
  1. +3 −3 src/labs/cellular_automata.clj
  2. +4 −4 src/labs/zero_sum.clj
6 src/labs/cellular_automata.clj
View
@@ -30,7 +30,7 @@
[:ol
[: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."
(showme launch-2)]
- [: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:"
+ [: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:"
(showme with-coords)]
[:li "Test " (c with-coords) " at the REPL:"
(repl-showme (with-coords (new-board [3 3])))]
@@ -52,7 +52,7 @@
[]
[[:h3 "Let 'Er Rip"]
[:ol
- [: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:"
+ [: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:"
(showme active-neighbors)]
[:li "Test " (c active-neighbors) " against a board that is all on:"
(repl-showme (active-neighbors (repeat (repeat :on))))]
@@ -61,7 +61,7 @@
[:li "An " (c :on) " cell goes to " (c :dying)]
[:li "A " (c :dying) " cell goes to " (c :off)]
[:li "An " (c :off) " cell with two active (" (c :on) ") neighbors goes to " (c :on)]
- [:li "All other cells go to off"]]
+ [:li "All other cells go to " (c: off)]]
"Implement " (c brians-brain-rules) " as a function of three rows."
(showme brians-brain-rules)]
[:li "Test the rules at the REPL."
8 src/labs/zero_sum.clj
View
@@ -32,17 +32,17 @@
(showme accounts-1/total-balance)]
[: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."
(showme accounts-1/transfer)]
- [:li "Create a " (c balance) " function that takes " (c accounts) " and an " (c accound-id) " and returns the current balance for an account."
+ [:li "Create a " (c balance) " function that takes " (c accounts) " and an " (c account-id) " and returns the current balance for an account."
(showme accounts-1/balance)]
[: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."]]])
(defn is-it-threadsafe
[]
- [[:h3 "Is Is Threadsafe?"]
+ [[:h3 "Is It Threadsafe?"]
[:p "No."]
[: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."]
[:ol
- [: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."
+ [: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."
(showme accounts-1/random-account-ids)]
[: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) " :"
(showme accounts-1/random-transfer)]
@@ -64,7 +64,7 @@
[: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."
(showme accounts-2/total-balance)
"Now you should be able to run as long a " (c trial) " as you like without a problem."]]
- [: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."]])
+ [: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."]])
(defn granularity
[]
Please sign in to comment.
Something went wrong with that request. Please try again.