Skip to content

Commit e10099a

Browse files
committed
Merging from the official book repository
1 parent 05f6545 commit e10099a

File tree

168 files changed

+65970
-7701
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+65970
-7701
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/Programming-Clojure.iml
32
/build.clj
43
/hello.out

classes/examples/tasklist.class

-5.61 KB
Binary file not shown.
-3.71 KB
Binary file not shown.

clojurebreaker/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: lein run

clojurebreaker/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# clojurebreaker
2+
3+
A website written in noir.
4+
5+
## Usage
6+
7+
```bash
8+
lein deps
9+
lein run
10+
```
11+
12+
## License
13+
14+
Copyright (C) 2011 FIXME
15+
16+
Distributed under the Eclipse Public License, the same as Clojure.
17+

clojurebreaker/project.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; START: clojurebreaker-project
2+
(defproject clojurebreaker "0.1.0-SNAPSHOT"
3+
:description "Clojurebreaker game for Programming Clojure 2nd Edition"
4+
:dependencies [[org.clojure/clojure "1.3.0"]
5+
[org.clojure/math.combinatorics "0.0.1"]
6+
[org.clojure/test.generative "0.1.3"]
7+
[noir "1.2.0"]]
8+
:main clojurebreaker.server)
9+
; END: clojurebreaker-project
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
html {
2+
margin:0;
3+
padding:0;
4+
border:0;
5+
}
6+
7+
body, div, span, object, iframe,
8+
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
9+
a, abbr, acronym, address, code,
10+
del, dfn, em, img, q, dl, dt, dd, ol, ul, li,
11+
fieldset, form, label, legend,
12+
table, caption, tbody, tfoot, thead, tr, th, td,
13+
article, aside, dialog, figure, footer, header,
14+
hgroup, nav, section {
15+
margin: 0;
16+
padding: 0;
17+
border: 0;
18+
font-weight: inherit;
19+
font-style: inherit;
20+
font-size: 100%;
21+
font-family: inherit;
22+
vertical-align: baseline;
23+
}
24+
25+
article, aside, dialog, figure, footer, header,
26+
hgroup, nav, section {
27+
display:block;
28+
}
29+
30+
body {
31+
line-height: 1.5;
32+
background: white;
33+
}
34+
35+
table {
36+
border-collapse: separate;
37+
border-spacing: 0;
38+
}
39+
40+
caption, th, td {
41+
text-align: left;
42+
font-weight: normal;
43+
float:none !important;
44+
}
45+
table, th, td {
46+
vertical-align: middle;
47+
}
48+
49+
blockquote:before, blockquote:after, q:before, q:after { content: ''; }
50+
blockquote, q { quotes: "" ""; }
51+
52+
a img { border: none; }
53+
54+
/*:focus { outline: 0; }*/
55+
56+
57+

clojurebreaker/scoring-table

Lines changed: 65540 additions & 0 deletions
Large diffs are not rendered by default.

clojurebreaker/snippets.clj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
;; this file contains the intermediate steps in building the
2+
;; Clojurebreaker game. See the clojurebreaker/src directory for the
3+
;; completed code.
4+
5+
; START:exact-matches-shell
6+
(defn exact-matches
7+
"Given two collections, return the number of positions where
8+
the collections contain equal items."
9+
[c1 c2])
10+
; END:exact-matches-shell
11+
12+
; START:integers-closed
13+
(defspec closed-under-addition
14+
+'
15+
[^long a ^long b]
16+
(assert (integer? %)))
17+
; END:integers-closed
18+
19+
; START:incorrect-spec
20+
(defspec incorrect-spec
21+
+'
22+
[^long a ^long b]
23+
(assert (< a %))
24+
(assert (< b %)))
25+
; END:incorrect-spec
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(ns clojurebreaker.game
2+
(:use clojure.pprint)
3+
(:require [clojure.data :as data]
4+
[clojure.math.combinatorics :as comb]
5+
[clojure.java.io :as io]))
6+
7+
;; START:exact-matches
8+
(defn exact-matches
9+
"Given two collections, return the number of positions where
10+
the collections contain equal items."
11+
[c1 c2]
12+
(let [[_ _ matches] (data/diff c1 c2)]
13+
(count (remove nil? matches))))
14+
;; END:exact-matches
15+
16+
;; START:unordered-matches
17+
(defn unordered-matches
18+
"Given two collections, return a map where each key is an item
19+
in both collections, and each value is the number of times the
20+
value occurs in the collection with fewest occurrences."
21+
[c1 c2]
22+
(let [f1 (select-keys (frequencies c1) c2)
23+
f2 (select-keys (frequencies c2) c1)]
24+
(merge-with min f1 f2)))
25+
;; END:unordered-matches
26+
27+
;; START:score
28+
(defn score
29+
[c1 c2]
30+
(let [exact (exact-matches c1 c2)
31+
unordered (apply + (vals (unordered-matches c1 c2)))]
32+
{:exact exact :unordered (- unordered exact)}))
33+
;; END: score
34+
35+
;; START: generate-turn-inputs
36+
(defn generate-turn-inputs
37+
"Generate all possible turn inputs for a clojurebreaker game
38+
with colors and n columns"
39+
[colors n]
40+
(-> (comb/selections colors n)
41+
(comb/selections 2)))
42+
;; END: generate-turn-inputs
43+
44+
;; START: score-inputs
45+
(defn score-inputs
46+
"Given a sequence of turn inputs, return a lazy sequence of
47+
maps with :secret, :guess, and :score."
48+
[inputs]
49+
(map
50+
(fn [[secret guess]]
51+
{:secret (seq secret)
52+
:guess (seq guess)
53+
:score (score secret guess)})
54+
inputs))
55+
;; END: score-inputs
56+
57+
(->> (generate-turn-inputs [:r :g :b] 2)
58+
(score-inputs))
59+
60+
;; step 17 score-table
61+
#_(score-all-games [:R :G :B] 3)
62+
63+
;; step 18 could check either the clj or the tabular form of score-all-games
64+
;; into source control and use it as a regression test
65+
;; (add clojure.java.io)
66+
(use 'clojure.pprint)
67+
(with-open [w (io/writer "scoring-table")]
68+
(binding [*out* w]
69+
(print-table (->> (generate-turn-inputs [:r :g :b :y] 4)
70+
(score-inputs)))))

0 commit comments

Comments
 (0)