Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: melissavoegeli/threading-in-clojure
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: clojurebridge-minneapolis/track2-threading
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 6 commits
  • 6 files changed
  • 3 contributors

Commits on Feb 22, 2015

  1. Added LICENSE, .gitignore

    tmarble committed Feb 22, 2015
    Copy the full SHA
    13512ca View commit details

Commits on Mar 6, 2015

  1. change assignments to fix broken code

    Melissa Voegeli authored and Melissa Voegeli committed Mar 6, 2015
    Copy the full SHA
    a70edec View commit details

Commits on Mar 7, 2015

  1. add hint

    melissavoegeli committed Mar 7, 2015
    Copy the full SHA
    9656862 View commit details
  2. agents change

    melissavoegeli committed Mar 7, 2015
    Copy the full SHA
    c97eca6 View commit details
  3. atoms hint

    melissavoegeli committed Mar 7, 2015
    Copy the full SHA
    f57edd1 View commit details

Commits on Jun 23, 2015

  1. pointer to installfest

    tmarble committed Jun 23, 2015
    Copy the full SHA
    acda4b5 View commit details
Showing with 97 additions and 70 deletions.
  1. +10 −0 .gitignore
  2. +19 −0 LICENSE
  3. +11 −0 README.md
  4. +15 −19 lib/agents.clj
  5. +15 −19 lib/atoms.clj
  6. +27 −32 lib/refs.clj
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# .gitignore

# Editor artifacts
.nrepl-port
*~

## Other
# silly Mac filesystem
*.DS_Store*
*._*
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# track2-threading

**NOTE:** Pointers to our latest version of the curriculum can be found in the [installfest](https://github.com/clojurebridge-minneapolis/installfest) repo!

## Overview Threading in Clojure

### Abstract
@@ -58,3 +62,10 @@ Rules #3 and #4 talk about something called mutable data. Mutable data means the
When your program has race conditions, it is almost _impossible_ to reproduce the bug in order to fix it. is generally scares programmers away from writing multithreaded programs.

One important thing about Clojure is that it inherently allows us to protects us from this problem right out of the box. We'll explore this idea a bit more later. For now lets continue on to learning about the **[Bank Account Feature](Bank_Account_Feature.md)** we'll be implementing to learn about multithreaded programming in Clojure.


## Copyright and License

Copyright © 2015 Melissa Voegeli

Licensed under the [MIT](http://opensource.org/licenses/MIT) [LICENSE](LICENSE)
34 changes: 15 additions & 19 deletions lib/agents.clj
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
; HINT: line 2, 26, and 37
(def my-account {:checking 5})

(defn balance "Print out the balance of the account" [account-hash] (:checking account-hash))

(defn withdrawal
"Subtract from checking balance"
[account-hash amount-to-withdraw] (assoc account-hash :checking (- (:checking account-hash) amount-to-withdraw)))

(defn alert-invalid-withdrawl
"Alert the user that the withdrawl cannot take place"
[] (println "Insufficient funds!"))

(defn withdraw?
"Check if balance is valid and withdraw the amount"
[account-hash amount-to-withdraw] (if (<= amount-to-withdraw (balance account-hash) )
(!IMPLEMENT-WITHDRAWL! account-hash amount-to-withdraw)
(withdrawal account-hash amount-to-withdraw)
(do
(alert-invalid-withdrawl)
account-hash)))

(defn print-balances
"Print the value known before the swap! happens if it is different from the known global balance"
[known-balance account-changes]
(if-not (= known-balance (balance @my-account-atom))
(println (str "Known Balance Beginning of Swap: $" known-balance ", Actual Global Balance: $" (balance @my-account-atom)))))
(if-not (= known-balance (balance my-account))
(println (str "Known Balance Beginning of Swap: $" known-balance ", Actual Global Balance: $" (balance my-account)))))

(defn sleep-withdraw-print
"Sleep 1 second then make a withdrawl and print out the balance known before and after"
@@ -25,28 +34,15 @@
(print-balances known-balance account-changes)
account-changes)))

(defn balance "Print out the balance of the account" [account-hash] (:checking account-hash))
(defn send-values "Calls sleep-withdraw-print on an account atom using swap" [] (sleep-withdraw-print my-account 1))

(defn loop-over
"Given an account, withdraw money however many times is specified"
[account num-of-times amount-to-withdraw]
(doseq [i (range num-of-times)]
(do
;(!IMPLEMENT-SEND!)
;(!IMPLEMENT-SEND-SHOWING-COLLISIONS!)
(send-values)
(println "Tried to withdraw $1"))))

; 1) Create the agent that references a map whose checking balance is currently $5

; 2) Subtract the amount from the checking balance. Implement the missing piece of the function.
(defn !IMPLEMENT-WITHDRAWL!
"Subtract from checking balance"
[account-hash amount-to-withdraw] ___________________________________________________________________________)

; 3) Demonstrate the synchronousness of withdrawing using atoms
(defn !IMPLEMENT-SEND! "Calls sleep-withdraw-print on an account atom using swap" [] ______________________________________________)

; 4) Demonstrate the collisions that can occur from an atom's "compare-and-set" mechanism using multiple simulataneous withdrawls
(defn !IMPLEMENT-SEND-SHOWING-COLLISIONS! "Does the same thing as !IMPLEMENT-WITHDRAWL! except in multiple threads" [] _______________________________________________________)

; 5) Print the known balance before, then use loop-over to withdraw money multiple times
(loop-over my-account 10 1)
34 changes: 15 additions & 19 deletions lib/atoms.clj
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
; HINT: line 2, 26, and 37
(def my-account {:checking 5})

(defn balance "Print out the balance of the account" [account-hash] (:checking account-hash))

(defn withdrawal
"Subtract from checking balance"
[account-hash amount-to-withdraw] (assoc account-hash :checking (- (:checking account-hash) amount-to-withdraw)))

(defn alert-invalid-withdrawl
"Alert the user that the withdrawl cannot take place"
[] (println "Insufficient funds!"))

(defn withdraw?
"Check if balance is valid and withdraw the amount"
[account-hash amount-to-withdraw] (if (<= amount-to-withdraw (balance account-hash) )
(!IMPLEMENT-WITHDRAWL! account-hash amount-to-withdraw)
(withdrawal account-hash amount-to-withdraw)
(do
(alert-invalid-withdrawl)
account-hash)))

(defn print-balances
"Print the value known before the swap! happens if it is different from the known global balance"
[known-balance account-changes]
(if-not (= known-balance (balance @my-account-atom))
(println (str "Known Balance Beginning of Swap: $" known-balance ", Actual Global Balance: $" (balance @my-account-atom)))))
(if-not (= known-balance (balance my-account))
(println (str "Known Balance Beginning of Swap: $" known-balance ", Actual Global Balance: $" (balance my-account)))))

(defn sleep-withdraw-print
"Sleep 1 second then make a withdrawl and print out the balance known before and after"
@@ -25,28 +34,15 @@
(print-balances known-balance account-changes)
account-changes)))

(defn balance "Print out the balance of the account" [account-hash] (:checking account-hash))
(defn swap-values "Calls sleep-withdraw-print on an account atom using swap" [] (sleep-withdraw-print my-account 1))

(defn loop-over
"Given an account, withdraw money however many times is specified"
[account num-of-times amount-to-withdraw]
(doseq [i (range num-of-times)]
(do
;(!IMPLEMENT-SWAP!)
;(!IMPLEMENT-SWAP-SHOWING-COLLISIONS!)
(swap-values)
(println "Tried to withdraw $1"))))

; 1) Create the atom that references a map whose checking balance is currently $5

; 2) Subtract the amount from the checking balance. Implement the missing piece of the function.
(defn !IMPLEMENT-WITHDRAWL!
"Subtract from checking balance"
[account-hash amount-to-withdraw] ___________________________________________________________________________)

; 3) Demonstrate the synchronousness of withdrawing using atoms
(defn !IMPLEMENT-SWAP! "Calls sleep-withdraw-print on an account atom using swap" [] ______________________________________________)

; 4) Demonstrate the collisions that can occur from an atom's "compare-and-set" mechanism using multiple simulataneous withdrawls
(defn !IMPLEMENT-SWAP-SHOWING-COLLISIONS! "Does the same thing as !IMPLEMENT-WITHDRAWL! except in multiple threads" [] _______________________________________________________)

; 5) Print the known balance before, then use loop-over to withdraw money multiple times
(loop-over my-account 10 1)
59 changes: 27 additions & 32 deletions lib/refs.clj
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
; Create two accounts with refs
____
____
; HINT: The lines you need to change are 3,4,11,18, and 34

(defn balance [acc] (:checking @acc))
(defn check-and-withdraw [account val]
(let [known-balance (balance account)]
(Thread/sleep 1000)
(let [worked (if (<= val (:checking @account) )
; Alter the ref and return true at the end
____
(do
(println "Insufficient funds!")
false))]
(println (str "Withdrawal Known Balance: " known-balance ", Current Balance: " (balance account)))
worked)))
(defn show-and-deposit [account val]
(let [known-balance (balance account)]
(Thread/sleep 1000)
; Alter the account and return the account at the end
(let [new-account ____]
(println (str "Deposit Known Balance: " known-balance ", Current Balance: " (balance new-account)))
new-account)))
(def account-a-atom (atom {:checking 100}))
(def account-b-atom (atom {:checking 100}))

(defn withdraw [acc val]
(check-and-withdraw acc val))
(defn balance [acc] (:checking acc))
(defn withdraw [account val]
"withdraw from the account, return true when successful"
(if (<= val (:checking @account) )
(do
(swap! account assoc :checking (- (:checking @account) val))
true)
(do
(println "Insufficient funds!")
false)))

(defn deposit [acc val]
(show-and-deposit acc val))
(defn deposit [account val]
(swap! account assoc :checking (+ (:checking @account) val)))

(defn transfer [from to val]
(Thread/sleep 1000)
(let [withdrawal-successful? (withdraw from val)]
(if withdrawal-successful?
(do
(deposit to val)
(println (str "Transfer Successful. From Balance: " (balance from) ", To Balance: " (balance to))))
(println (str "Transfer Unsuccessful. From Balance: " (balance from) ", To Balance: " (balance to))))))
(do
(deposit to val)
(println (str "Transfer Successful. From Balance: " (balance @from) ", To Balance: " (balance @to))))
(println (str "Transfer Unsuccessful. From Balance: " (balance @from) ", To Balance: " (balance @to))))))

(defn loop-over
"Given an account, withdraw money however many times is specified"
[num-of-times amount-to-withdraw]
(doseq [i (range num-of-times)]
(do
(future (transfer account-a-atom account-b-atom amount-to-withdraw)))))

; Create a thread that contains a transaction to transfer $1 from account a to account b
____
(loop-over 10 60)