Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 14, 2016
0 parents commit 5501b50
Show file tree
Hide file tree
Showing 31 changed files with 633 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE.txt
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.


15 changes: 15 additions & 0 deletions README.md
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 added __MACOSX/._sierra_clojure_code
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch07_even_odd.clj
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch09_game.clj
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch10_command_line
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch10_xml_parser.clj
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch13_pair_type.clj
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch13_payroll.clj
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch14_gcd.clj
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/._ch14_transients.clj
Binary file not shown.
Binary file added __MACOSX/sierra_clojure_code/ch10_command_line/._com
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 14 additions & 0 deletions contributing.md
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 sierra_clojure_code/Ch03VanderHart_ProgramFlow_Code.clj
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))))





31 changes: 31 additions & 0 deletions sierra_clojure_code/Ch05VanderHart_Sequences_Code.clj
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))
73 changes: 73 additions & 0 deletions sierra_clojure_code/Ch06VanderHart_StateManage.clj
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 sierra_clojure_code/Ch12VanderHart_Metaprogramming_Code.clj
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))

19 changes: 19 additions & 0 deletions sierra_clojure_code/ch07_even_odd.clj
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))))

0 comments on commit 5501b50

Please sign in to comment.