Mercurial > coderloop
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/scientist2.clj Tue Oct 18 01:17:49 2011 -0700 1.3 @@ -0,0 +1,197 @@ 1.4 +(ns coderloop.scientist2 1.5 + (:refer-clojure :only []) 1.6 + (:require rlm.ns-rlm rlm.light-base)) 1.7 +(rlm.ns-rlm/ns-clone rlm.light-base) 1.8 + 1.9 + 1.10 +(use 'clojure.contrib.sql) 1.11 +(import 'org.gjt.mm.mysql.Driver) 1.12 +(use 'clojure.contrib.sql) 1.13 +(undef distinct case compile drop take sort) 1.14 +(use 'clojureql.core) 1.15 +(import 'java.io.File) 1.16 +(use '[clojure.java.io :only [copy]]) 1.17 +(use '[clojure.contrib.shell-out :only [sh]]) 1.18 +(use '[clojure.string :only [split]]) 1.19 +(use '[coderloop [utils :only [md5]]]) 1.20 +(import 'java.sql.Date) 1.21 + 1.22 + 1.23 + 1.24 +(def *port* 3306) 1.25 + 1.26 +(def *host* "localhost") 1.27 + 1.28 +(def *db-host* "localhost") 1.29 + 1.30 +(def *db-name* "clojure_rlm") 1.31 + 1.32 +(def *db* {:classname "com.mysql.jdbc.Driver" 1.33 + :subprotocol "mysql" 1.34 + :subname (str "//" *db-host* ":" *port* "/" *db-name*) 1.35 + :user "root" 1.36 + :password "sql1005025"}) 1.37 + 1.38 +(def databases [:essay_scientists :scientists :ideas :scientist_whereabouts]) 1.39 + 1.40 + 1.41 + 1.42 + 1.43 +(declare essays essay century contemps data eric scientists date->str date) 1.44 + 1.45 +(defn scientists [] 1.46 + @(table *db* :essay_scientists)) 1.47 + 1.48 + 1.49 +(defn contemps [s birth death] 1.50 + (sort-by (fn [s] [(:surname s) (:name s)]) 1.51 + (map #(select-keys % [:name :surname]) 1.52 + @(select 1.53 + (table *db* :scientists) 1.54 + (where 1.55 + (and 1.56 + (< :birth_date death) 1.57 + (< birth :death_date) 1.58 + (not (= :name (:name s))))))) )) 1.59 + 1.60 +(defn data [s] 1.61 + (let [locations 1.62 + (sort-by 1.63 + :immigration_date 1.64 + (map #(select-keys % [:immigration_date :emigration_date :country]) 1.65 + @(select 1.66 + (table *db* :scientist_whereabouts) 1.67 + (where 1.68 + (and 1.69 + (= :scientist_whereabouts.surname (:surname s)) 1.70 + (= :scientist_whereabouts.name (:name s))))))) 1.71 + ideas 1.72 + (sort-by 1.73 + #(.toLowerCase %) 1.74 + (map :idea 1.75 + @(select 1.76 + (table *db* :ideas) 1.77 + (where 1.78 + (and 1.79 + (= :ideas.surname (:surname s)) 1.80 + (= :ideas.name (:name s))))))) 1.81 + data 1.82 + (first 1.83 + @(select 1.84 + (table *db* :scientists) 1.85 + (where 1.86 + (and 1.87 + (= :scientists.surname (:surname s)) 1.88 + (= :scientists.name (:name s)))))) 1.89 + birth (:birth_date data) 1.90 + death (:death_date data) 1.91 + contemp (contemps s birth death)] 1.92 + (merge data {:locations locations :contemps contemp :ideas ideas}))) 1.93 + 1.94 +(defn date->str [#^java.sql.Date d] 1.95 + (format "%02d/%02d/%04d" (.getDate d) (inc (.getMonth d)) (+ 1900 (.getYear d)))) 1.96 + 1.97 +(def strcat (partial apply str)) 1.98 + 1.99 +(defn century [#^java.sql.Date d] 1.100 + (* 100 (clojure.core/unchecked-divide (+ 1900 (.getYear d)) 100))) 1.101 + 1.102 +(defn essay-map [s] 1.103 + (let [base (data s) 1.104 + locations 1.105 + (strcat 1.106 + (interpose 1.107 + ", " 1.108 + (map (fn [{:keys [emigration_date immigration_date country ]}] 1.109 + (str country " from " (date->str immigration_date) " to " 1.110 + (date->str emigration_date))) (:locations base)))) 1.111 + contemps 1.112 + (strcat 1.113 + (interpose 1.114 + ", " 1.115 + (map (fn [{:keys [name surname]}] 1.116 + (str name " " surname)) 1.117 + (:contemps base)))) 1.118 + birth-date (date->str (:birth_date base)) 1.119 + death-date (date->str (:death_date base)) 1.120 + century (century (:birth_date base)) 1.121 + lifespan 1.122 + (str "[" 1.123 + (:birth_place base) " " birth-date ", " 1.124 + (:death_place base) " " death-date 1.125 + "]") 1.126 + ideas (strcat (interpose ", " (:ideas base))) 1.127 + ] 1.128 + {:lifespan lifespan :name (:name base) :surname (:surname base) 1.129 + :contemps contemps :locations locations :century century :ideas ideas})) 1.130 + 1.131 +(defn essay [s] 1.132 + (let [{:keys [lifespan name surname contemps locations century ideas]} (essay-map s)] 1.133 + (str name " " surname " " lifespan " is one of the most famous scientists of " 1.134 + century ". " 1.135 + "Between all " name "'s ideas we mention: " ideas ". " 1.136 + name " lived in: " locations ". " 1.137 + name " was a contemporary of " contemps ".\n\n"))) 1.138 + 1.139 +(defn essays [] 1.140 + (map essay (scientists))) 1.141 + 1.142 +(defn print-essays [] 1.143 + (dorun (map print (essays)))) 1.144 + 1.145 +(defn essays->str [] 1.146 + (strcat (essays))) 1.147 + 1.148 +(defn main [[username password db-name]] 1.149 + (let [db-host *host* 1.150 + db-port *port* 1.151 + db {:classname "com.mysql.jdbc.Driver" 1.152 + :subprotocol "mysql" 1.153 + :subname (str "//" db-host ":" db-port "/" db-name) 1.154 + :user username 1.155 + :password password}] 1.156 + (binding [*db* db] 1.157 + (print-essays)))) 1.158 + 1.159 +(if (command-line?) 1.160 + (main *command-line-args*)) 1.161 + 1.162 + 1.163 + 1.164 + 1.165 + 1.166 +(def eric {:name "Erik 3802" :surname "Green 3802"}) 1.167 +(def date (java.sql.Date. 4564654645)) 1.168 + 1.169 + 1.170 + 1.171 +(defvar error-person 1.172 + {:name "Albert 3850" :surname "Newton 3850"} 1.173 + "this is the person in in which John 5750 is supposedly 1.174 + wrongly included my code says they're contempoaries, 1.175 + but the expected output disaggrees ") 1.176 + 1.177 +(defvar wrong-include 1.178 + {:name "John 5750" :surname "Newton 5750"} 1.179 + "this one is inculded and supposedly should not be.") 1.180 + 1.181 +;;coderloop.scientist2> (select-keys (data error-person) [:birth_date :death_date]) 1.182 +;;{:death_date #<Date 0226-10-18>, :birth_date #<Date 0166-10-16>} 1.183 +;;coderloop.scientist2> (select-keys (data wrong-include) [:birth_date :death_date]) 1.184 +;;{:death_date #<Date 0226-01-01>, :birth_date #<Date 0166-12-30>} 1.185 + 1.186 +;; these exibit the same problem. They appear to be contemps, 1.187 +;; but the expected output says that they're not! 1.188 +(defvar error-person2 1.189 + {:name "Erik 3851" :surname "Smith 3851"}) 1.190 + 1.191 +(defvar wrong-include2 1.192 + {:name "Gil 5751" :surname "Smith 5751"}) 1.193 + 1.194 +;;coderloop.scientist2> (select-keys (data error-person2) [:birth_date :death_date]) 1.195 +;;{:death_date #<Date 0227-10-19>, :birth_date #<Date 0167-10-17>} 1.196 +;;coderloop.scientist2> (select-keys (data wrong-include2) [:birth_date :death_date]) 1.197 +;;{:death_date #<Date 0227-01-02>, :birth_date #<Date 0167-12-31>} 1.198 + 1.199 + 1.200 +