Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns query-1
- (:require [clojure.java.io :as io]
- [clojure.string :as string]
- ))
- ; check if the city has more than 0.5E6 people
- (def csv-file "data/ca.csv")
- ;;reading csv file into hashmap clojure
- (defn parse-record [line]
- (let [x line
- [city lat long country iso2 admin capital pop poppropper]
- (string/split x #",")]
- {:city city
- :lat (read-string lat)
- :long (read-string long)
- :country country
- :iso2 iso2
- :admin admin
- :capital capital
- :pop (read-string pop)
- :popproper poppropper}))
- (defn cities []
- (with-open [rdr (io/reader csv-file)]
- (vec
- (for [line (drop 1 (line-seq rdr))]
- (parse-record line)))))
- (defn city
- ([name xs]
- (->> xs
- (filter #(= (:city %) name))
- first))
- ([name] (city (cities))))
- (defn distance [record1 record2]
- (let [R 6371
- dlat (Math/toRadians (- (:lat record2) (:lat record1)))
- dlon (Math/toRadians (- (:long record2) (:long record1)))
- lolnice (Math/toRadians (:lat record1))
- dnice (Math/toRadians (:lat record2))
- a (+ (* (Math/sin (/ dlat 2))
- (Math/sin (/ dlat 2)))
- (* (Math/sin (/ dlon 2))
- (Math/sin (/ dlon 2))
- (Math/cos lolnice)
- (Math/cos dnice)))]
- (* R 2 (Math/asin (Math/sqrt a)))))
- (defn large [city-record]
- (> (:pop city-record) 0.5E6))
- (defn close [c1 c2]
- (< (distance c1 c2) 600))
- (defn closest-city-pairs []
- (let [large-cities (->> (cities) (filter large) vec)]
- (for [x (butlast large-cities)
- y (rest large-cities)
- :when (and (not= x y)
- (close x y))]
- [(:city x) (:city y)])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement