Guest User

Untitled

a guest
Nov 15th, 2019
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns query-1
  2.   (:require [clojure.java.io :as io]
  3.             [clojure.string :as string]
  4.         ))
  5. ; check if the city has more than 0.5E6 people
  6. (def csv-file "data/ca.csv")
  7.  
  8. ;;reading csv file into hashmap clojure
  9.  
  10. (defn parse-record [line]
  11.     (let [x line
  12.           [city lat long country iso2 admin capital pop poppropper]
  13.           (string/split x #",")]
  14.     {:city city
  15.      :lat  (read-string lat)
  16.      :long (read-string long)
  17.      :country country
  18.      :iso2  iso2
  19.      :admin  admin
  20.      :capital capital
  21.      :pop   (read-string pop)
  22.      :popproper poppropper}))
  23.  
  24. (defn cities []
  25.   (with-open [rdr (io/reader csv-file)]
  26.     (vec
  27.      (for [line (drop 1 (line-seq rdr))]
  28.        (parse-record line)))))
  29.  
  30. (defn city
  31.   ([name xs]
  32.    (->> xs
  33.         (filter #(= (:city %) name))
  34.         first))
  35.   ([name] (city (cities))))
  36.  
  37. (defn distance [record1 record2]
  38.   (let [R 6371
  39.         dlat (Math/toRadians (- (:lat record2) (:lat record1)))
  40.         dlon (Math/toRadians (- (:long record2) (:long record1)))
  41.         lolnice (Math/toRadians (:lat record1))
  42.         dnice (Math/toRadians (:lat record2))
  43.         a (+ (* (Math/sin (/ dlat 2))
  44.                 (Math/sin (/ dlat 2)))
  45.              (* (Math/sin (/ dlon 2))
  46.                 (Math/sin (/ dlon 2))
  47.                 (Math/cos lolnice)
  48.                 (Math/cos dnice)))]
  49.     (* R 2 (Math/asin (Math/sqrt a)))))
  50.  
  51. (defn large [city-record]
  52.   (> (:pop city-record)  0.5E6))
  53.  
  54. (defn close [c1 c2]
  55.   (< (distance c1 c2) 600))
  56.  
  57. (defn closest-city-pairs []
  58.   (let [large-cities (->> (cities) (filter large) vec)]
  59.     (for [x (butlast large-cities)
  60.           y (rest    large-cities)
  61.           :when (and (not= x y)
  62.                      (close x y))]
  63.       [(:city x) (:city y)])))
RAW Paste Data