-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 14, 2016
0 parents
commit 5501b50
Showing
31 changed files
with
633 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2010 Luke VanderHart and Stuart Sierra | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to 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 OR APRESS 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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#Apress Source Code | ||
|
||
This repository accompanies [*Practical Clojure*](http://www.apress.com/9781430272311) by Luke VanderHart and Stuart Sierra (Apress, 2010). | ||
|
||
![Cover image](9781430272311.jpg) | ||
|
||
Download the files as a zip using the green button, or clone the repository to your machine using Git. | ||
|
||
##Releases | ||
|
||
Release v1.0 corresponds to the code in the published book, without corrections or updates. | ||
|
||
##Contributions | ||
|
||
See the file Contributing.md for more information on how you can contribute to this repository. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+511 Bytes
__MACOSX/sierra_clojure_code/._Ch12VanderHart_Metaprogramming_Code.clj
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+193 Bytes
__MACOSX/sierra_clojure_code/ch10_command_line/com/example/._app.clj
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Contributing to Apress Source Code | ||
|
||
Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. | ||
|
||
## How to Contribute | ||
|
||
1. Make sure you have a GitHub account. | ||
2. Fork the repository for the relevant book. | ||
3. Create a new branch on which to make your change, e.g. | ||
`git checkout -b my_code_contribution` | ||
4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. | ||
5. Submit a pull request. | ||
|
||
Thank you for your contribution! |
134 changes: 134 additions & 0 deletions
134
sierra_clojure_code/Ch03VanderHart_ProgramFlow_Code.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
;; Example code for Chapter 3 | ||
;; Author: Luke VanderHart | ||
|
||
;; Snippet: weather-judge function | ||
(defn weather-judge | ||
"Given a temperature in degrees centigrade, comments on the weather." | ||
[temp] | ||
(cond | ||
(< temp 20) "It's cold" | ||
(> temp 25) "It's hot" | ||
:else "It's comfortable")) | ||
|
||
;; Snippet: seconds-to-weeks function | ||
(defn seconds-to-weeks | ||
"Converts seconds to weeks" | ||
[seconds] | ||
(/ (/ (/ (/ seconds 60) 60) 24) 7)) | ||
|
||
;; Snippet: expanded seconds-to-weeks function | ||
(defn seconds-to-weeks | ||
"Converts seconds to weeks" | ||
[seconds] | ||
(let [minutes (/ seconds 60) | ||
hours (/ minutes 60) | ||
days (/ hours 24) | ||
weeks (/ days 7)] | ||
weeks)) | ||
|
||
;; Listing 3-1, Calculating Square Roots | ||
(defn abs | ||
"Calculates the absolute value of a number" | ||
[n] | ||
(if (< n 0) | ||
(* -1 n) | ||
n)) | ||
|
||
(defn avg | ||
"returns the average of two arguments" | ||
[a b] | ||
(/ (+ a b) 2)) | ||
|
||
(defn good-enough? | ||
"Tests if a guess is close enough to the real square root" | ||
[number guess] | ||
(let [diff (- (* guess guess) number)] | ||
(if (< (abs diff) 0.001) | ||
true | ||
false))) | ||
|
||
(defn sqrt | ||
"returns the square root of the supplied number" | ||
([number] (sqrt number 1.0)) | ||
([number guess] | ||
(if (good-enough? number guess) | ||
guess | ||
(sqrt number (avg guess (/ number guess)))))) | ||
|
||
;; Listing 3-2, Calculating Exponents | ||
(defn power | ||
"Calculates a number to the power of a provided exponent." | ||
[number exponent] | ||
(if (zero? exponent) | ||
1 | ||
(* number (power number (- exponent 1))))) | ||
|
||
;; Listing 3-3, Adding up numbers without Tail Recursion | ||
(defn add-up | ||
"adds all the numbers below a given limit" | ||
([limit] (add-up limit 0 0 )) | ||
([limit current sum] | ||
(if (< limit current) | ||
sum | ||
(add-up limit (+ 1 current) (+ current sum))))) | ||
|
||
;; Listing 3-4, Adding up Numbers Correctly with Tail-recursion | ||
(defn add-up | ||
"adds all the numbers up to a limit" | ||
([limit] (add-up limit 0 0 )) | ||
([limit current sum] | ||
(if (< limit current) | ||
sum | ||
(recur limit (+ 1 current) (+ current sum))))) | ||
|
||
;; Snippet, example of loop | ||
(loop [i 0] | ||
(if (= i 10) | ||
i | ||
(recur (+ i 1)))) | ||
|
||
|
||
;; Snippet, square root function using recur | ||
(defn sqrt | ||
"returns the square root of the supplied number" | ||
([number] (sqrt number 1.0)) | ||
([number guess] | ||
(if (good-enough? number guess) | ||
guess | ||
(recur number (avg guess (/ number guess)))))) | ||
|
||
;; Snippet, square root function using loop | ||
(defn loop-sqrt | ||
"returns the square root of the supplied number" | ||
[number] | ||
(loop [guess 1.0] | ||
(if (good-enough? number guess) | ||
guess | ||
(recur (avg guess (/ number guess)))))) | ||
|
||
;; Snippet, square function with side effects | ||
(defn square | ||
"Squares a number, with side effects." | ||
[x] | ||
(println "Squaring" x) | ||
(println "The return value will be" (* x x)) | ||
(* x x)) | ||
|
||
;; Snippet, arg-switch function | ||
(defn arg-switch | ||
"Applies the supplied function to the arguments in both possible orders. " | ||
[fun arg1 arg2] | ||
(list (fun arg1 arg2) (fun arg2 arg1))) | ||
|
||
;; Snippet, rangechecker function | ||
(defn rangechecker | ||
"Returns a function that determines if a number is in a provided range." | ||
[min max] | ||
(fn [num] | ||
(and (<= num max) | ||
(<= min num)))) | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
;; Example code for Chapter 5 | ||
;; Author: Luke VanderHart | ||
|
||
;; Snippet: printall function | ||
(defn printall [s] | ||
(if (not (empty? s)) | ||
(do | ||
(println (str "Item: " (first s))) | ||
(recur (rest s))))) | ||
|
||
|
||
;; Snippet: make-int-seq function | ||
(defn make-int-seq [max] | ||
(loop [acc nil n max] | ||
(if (zero? n) | ||
acc | ||
(recur (cons n acc) (dec n))))) | ||
|
||
;; Snippet: lazy-counter function | ||
(defn lazy-counter [base increment] | ||
(lazy-seq | ||
(cons base (lazy-counter (+ base increment) increment)))) | ||
|
||
|
||
;; Snippet: non-lazy counter function | ||
(defn counter [base increment] | ||
(cons base (counter (+ base increment) increment))) | ||
|
||
;; Snippet: lazy-counter-iterate function | ||
(defn lazy-counter-iterate [base increment] | ||
(iterate (fn [n] (+ n increment)) base)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
;; Example code for Chapter 6 | ||
;; Author: Luke VanderHart | ||
|
||
;; Listing 6-1, Bank Accounts in STM | ||
(def account1 (ref 1000)) | ||
(def account2 (ref 1500)) | ||
|
||
(defn transfer | ||
"transfers amount of money from a to b" | ||
[a b amount] | ||
(dosync | ||
(alter a - amount) | ||
(alter b + amount))) | ||
|
||
(transfer account1 account2 300) | ||
(transfer account2 account1 50) | ||
|
||
(println “Account #1:” @account1) | ||
(println “Account #2:” @account2) | ||
|
||
;; Listing 6-2, An Address Book in STM | ||
|
||
(def my-contacts (ref [])) | ||
|
||
(defn add-contact | ||
"adds a contact to the provided contact list" | ||
[contacts contact] | ||
(dosync | ||
(alter contacts conj (ref contact)))) | ||
|
||
(defn print-contacts | ||
"prints a list of contacts" | ||
[contacts] | ||
(doseq [c @contacts] | ||
(println (str "Name: " (@c :lname) ", " (@c :fname))))) | ||
|
||
(add-contact my-contacts {:fname "Luke" :lname "VanderHart"}) | ||
(add-contact my-contacts {:fname "Stuart" :lname "Sierra"}) | ||
(add-contact my-contacts {:fname "John" :lname "Doe"}) | ||
|
||
(print-contacts my-contacts) | ||
|
||
;; Listing 6-3, Adding Initials to the Address Book | ||
|
||
(defn add-initials | ||
"adds initials to a single contact and returns it" | ||
[contact] | ||
(assoc contact :initials | ||
(str (first (contact :fname)) (first (contact :lname))))) | ||
|
||
(defn add-all-initials | ||
"adds initials to each of the contacts in a list of contacts" | ||
[contacts] | ||
(dosync | ||
(doseq [contact (ensure contacts)] | ||
(alter contact add-initials)))) | ||
|
||
(defn print-contacts-and-initials | ||
"prints a list of contacts, with initials" | ||
[contacts] | ||
(dorun (map (fn [c] | ||
(println (str "Name: " (@c :lname) ", " (@c :fname) " (" (@c :initials) ")"))) @contacts))) | ||
|
||
(defn print-contacts-and-initials | ||
"prints a list of contacts, with initials" | ||
[contacts] | ||
(doseq [c @contacts] | ||
(println (str "Name: " (@c :lname) ", " (@c :fname) " (" (@c :initials) ")")))) | ||
|
||
(add-all-initials my-contacts) | ||
(print-contacts-and-initials my-contacts) | ||
|
||
|
54 changes: 54 additions & 0 deletions
54
sierra_clojure_code/Ch12VanderHart_Metaprogramming_Code.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
;; Example code for Chapter 12 | ||
;; Author: Luke VanderHart | ||
|
||
;; Snippit, triple-do macro | ||
(defmacro triple-do [form] | ||
(list 'do form form form)) | ||
|
||
;; Snippit, infix macro | ||
(defmacro infix [form] | ||
(cons (second form) (cons (first form) (nnext form)))) | ||
|
||
;; Snippit, template-triple-do macro | ||
(defmacro template-triple-do [form] | ||
`(do ~form ~form ~form)) | ||
|
||
;; Snippit, template-infix macro (incorrect version) | ||
(defmacro template-infix [form] | ||
`(~(second form) ~(first form) ~(nnext form))) | ||
|
||
;; Snippit, template-infix macro (correct version) | ||
(defmacro template-infix [form] | ||
`(~(second form) ~(first form) ~@(nnext form))) | ||
|
||
;; Snippit, rand-expr macro | ||
(defmacro rand-expr [form1 form2] | ||
`(let [n# (rand-int 2)] | ||
(if (zero? n#) ~form1 ~form2))) | ||
|
||
;; Snippit, rand-expr-multi macro | ||
(defmacro rand-expr-multi [& exprs] | ||
`(let [ct# ~(count exprs)] | ||
(case (rand-int ct#) | ||
~@(interleave (range (count exprs)) exprs)))) | ||
|
||
;; Snippit, ++ macro | ||
(defmacro ++ | ||
[& exprs] | ||
(if (>= 2 (count exprs) | ||
`(+ ~@exprs) | ||
`(+ ~@(first exprs) (++ ~@(rest exprs)))))) | ||
|
||
;; Snippit, xml-helper function and xml macro | ||
(defn xml-helper [form] | ||
(if (not (seq? form)) | ||
(str form) | ||
(let [name (first form) | ||
children (rest form)] | ||
(str "<" name ">" | ||
(apply str (map xml-helper children)) | ||
"</" name ">")))) | ||
|
||
(defmacro xml [form] | ||
(xml-helper form)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
;;; ch07_even_odd.clj | ||
|
||
;; Luke Vanderhart and Stuart Sierra | ||
;; Practical Clojure | ||
|
||
;; Copyright (c) 2010 by Luke Vanderhart and Stuart Sierra | ||
|
||
|
||
;;; Forward Declarations (page 122) | ||
|
||
(declare is-even? is-odd?) | ||
|
||
(defn is-even? [n] | ||
(if (= n 2) true | ||
(is-odd? (dec n)))) | ||
|
||
(defn is-odd? [n] | ||
(if (= n 3) true | ||
(is-even? (dec n)))) |
Oops, something went wrong.