comparison src/scientist2.clj @ 0:307a81e46071 tip

initial committ
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Oct 2011 01:17:49 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:307a81e46071
1 (ns coderloop.scientist2
2 (:refer-clojure :only [])
3 (:require rlm.ns-rlm rlm.light-base))
4 (rlm.ns-rlm/ns-clone rlm.light-base)
5
6
7 (use 'clojure.contrib.sql)
8 (import 'org.gjt.mm.mysql.Driver)
9 (use 'clojure.contrib.sql)
10 (undef distinct case compile drop take sort)
11 (use 'clojureql.core)
12 (import 'java.io.File)
13 (use '[clojure.java.io :only [copy]])
14 (use '[clojure.contrib.shell-out :only [sh]])
15 (use '[clojure.string :only [split]])
16 (use '[coderloop [utils :only [md5]]])
17 (import 'java.sql.Date)
18
19
20
21 (def *port* 3306)
22
23 (def *host* "localhost")
24
25 (def *db-host* "localhost")
26
27 (def *db-name* "clojure_rlm")
28
29 (def *db* {:classname "com.mysql.jdbc.Driver"
30 :subprotocol "mysql"
31 :subname (str "//" *db-host* ":" *port* "/" *db-name*)
32 :user "root"
33 :password "sql1005025"})
34
35 (def databases [:essay_scientists :scientists :ideas :scientist_whereabouts])
36
37
38
39
40 (declare essays essay century contemps data eric scientists date->str date)
41
42 (defn scientists []
43 @(table *db* :essay_scientists))
44
45
46 (defn contemps [s birth death]
47 (sort-by (fn [s] [(:surname s) (:name s)])
48 (map #(select-keys % [:name :surname])
49 @(select
50 (table *db* :scientists)
51 (where
52 (and
53 (< :birth_date death)
54 (< birth :death_date)
55 (not (= :name (:name s))))))) ))
56
57 (defn data [s]
58 (let [locations
59 (sort-by
60 :immigration_date
61 (map #(select-keys % [:immigration_date :emigration_date :country])
62 @(select
63 (table *db* :scientist_whereabouts)
64 (where
65 (and
66 (= :scientist_whereabouts.surname (:surname s))
67 (= :scientist_whereabouts.name (:name s)))))))
68 ideas
69 (sort-by
70 #(.toLowerCase %)
71 (map :idea
72 @(select
73 (table *db* :ideas)
74 (where
75 (and
76 (= :ideas.surname (:surname s))
77 (= :ideas.name (:name s)))))))
78 data
79 (first
80 @(select
81 (table *db* :scientists)
82 (where
83 (and
84 (= :scientists.surname (:surname s))
85 (= :scientists.name (:name s))))))
86 birth (:birth_date data)
87 death (:death_date data)
88 contemp (contemps s birth death)]
89 (merge data {:locations locations :contemps contemp :ideas ideas})))
90
91 (defn date->str [#^java.sql.Date d]
92 (format "%02d/%02d/%04d" (.getDate d) (inc (.getMonth d)) (+ 1900 (.getYear d))))
93
94 (def strcat (partial apply str))
95
96 (defn century [#^java.sql.Date d]
97 (* 100 (clojure.core/unchecked-divide (+ 1900 (.getYear d)) 100)))
98
99 (defn essay-map [s]
100 (let [base (data s)
101 locations
102 (strcat
103 (interpose
104 ", "
105 (map (fn [{:keys [emigration_date immigration_date country ]}]
106 (str country " from " (date->str immigration_date) " to "
107 (date->str emigration_date))) (:locations base))))
108 contemps
109 (strcat
110 (interpose
111 ", "
112 (map (fn [{:keys [name surname]}]
113 (str name " " surname))
114 (:contemps base))))
115 birth-date (date->str (:birth_date base))
116 death-date (date->str (:death_date base))
117 century (century (:birth_date base))
118 lifespan
119 (str "["
120 (:birth_place base) " " birth-date ", "
121 (:death_place base) " " death-date
122 "]")
123 ideas (strcat (interpose ", " (:ideas base)))
124 ]
125 {:lifespan lifespan :name (:name base) :surname (:surname base)
126 :contemps contemps :locations locations :century century :ideas ideas}))
127
128 (defn essay [s]
129 (let [{:keys [lifespan name surname contemps locations century ideas]} (essay-map s)]
130 (str name " " surname " " lifespan " is one of the most famous scientists of "
131 century ". "
132 "Between all " name "'s ideas we mention: " ideas ". "
133 name " lived in: " locations ". "
134 name " was a contemporary of " contemps ".\n\n")))
135
136 (defn essays []
137 (map essay (scientists)))
138
139 (defn print-essays []
140 (dorun (map print (essays))))
141
142 (defn essays->str []
143 (strcat (essays)))
144
145 (defn main [[username password db-name]]
146 (let [db-host *host*
147 db-port *port*
148 db {:classname "com.mysql.jdbc.Driver"
149 :subprotocol "mysql"
150 :subname (str "//" db-host ":" db-port "/" db-name)
151 :user username
152 :password password}]
153 (binding [*db* db]
154 (print-essays))))
155
156 (if (command-line?)
157 (main *command-line-args*))
158
159
160
161
162
163 (def eric {:name "Erik 3802" :surname "Green 3802"})
164 (def date (java.sql.Date. 4564654645))
165
166
167
168 (defvar error-person
169 {:name "Albert 3850" :surname "Newton 3850"}
170 "this is the person in in which John 5750 is supposedly
171 wrongly included my code says they're contempoaries,
172 but the expected output disaggrees ")
173
174 (defvar wrong-include
175 {:name "John 5750" :surname "Newton 5750"}
176 "this one is inculded and supposedly should not be.")
177
178 ;;coderloop.scientist2> (select-keys (data error-person) [:birth_date :death_date])
179 ;;{:death_date #<Date 0226-10-18>, :birth_date #<Date 0166-10-16>}
180 ;;coderloop.scientist2> (select-keys (data wrong-include) [:birth_date :death_date])
181 ;;{:death_date #<Date 0226-01-01>, :birth_date #<Date 0166-12-30>}
182
183 ;; these exibit the same problem. They appear to be contemps,
184 ;; but the expected output says that they're not!
185 (defvar error-person2
186 {:name "Erik 3851" :surname "Smith 3851"})
187
188 (defvar wrong-include2
189 {:name "Gil 5751" :surname "Smith 5751"})
190
191 ;;coderloop.scientist2> (select-keys (data error-person2) [:birth_date :death_date])
192 ;;{:death_date #<Date 0227-10-19>, :birth_date #<Date 0167-10-17>}
193 ;;coderloop.scientist2> (select-keys (data wrong-include2) [:birth_date :death_date])
194 ;;{:death_date #<Date 0227-01-02>, :birth_date #<Date 0167-12-31>}
195
196
197