Guest User

Untitled

a guest
Nov 15th, 2019
132
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns query1 ;;ns is namespace
  2. (:gen-class)
  3. (:require [clojure.java.io :as io]
  4. [clojure.string :as string]
  5. ))
  6.  
  7. (defn reader []
  8. (io/reader "data/ca.csv"))
  9.  
  10. (defn parse-record [line]
  11. (let [y line
  12. [city lat long country iso2 admin capital pop poppropper]
  13. (string/split y #",")]
  14.  
  15. (into {} [[:city city]
  16. [:lat (read-string lat)]
  17. [:long (read-string long)]
  18. [:country country]
  19. [:iso2 iso2]
  20. [:admin admin]
  21. [:capital capital]
  22. [:pop (read-string pop)]
  23. [:popproper poppropper]]
  24. )
  25. ))
  26.  
  27.  
  28. (defn cities []
  29. (with-open [rdr (reader)]
  30. (vec
  31. (for [line (drop 1 (line-seq rdr))]
  32. (parse-record line)))))
  33.  
  34.  
  35. (defn city ([name var]
  36. (->> var
  37. (filter #(= (:city %) name))
  38. first))
  39. ([name] (city name (cities))))
  40.  
  41. (println " ")
  42. (println "City Toronto Hash Map")
  43. (print (city "Toronto"))
  44. (println " ")
  45.  
  46. (defn distance [record1 record2]
  47. (let [R 6371
  48. dlat (Math/toRadians (- (:lat record2) (:lat record1)))
  49. dlon (Math/toRadians (- (:lat record2) (:lat record1)))
  50. var1 (Math/toRadians (:lat record1))
  51. var2 (Math/toRadians (:lat record2))
  52. a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2))) (* (Math/sin (/ dlon 2)) (Math/sin (/ dlon 2)) (Math/cos var1) (Math/cos var2)))]
  53. (* R 2 (Math/asin (Math/sqrt a)))))
  54.  
  55. (defn large [city-record]
  56. (let [newVec (:pop city-record)]
  57. (> newVec 0.5E6)))
  58.  
  59. (println " ")
  60. (println "Large?")
  61. (println (large (city "Toronto")))
  62. (println " ")
  63.  
  64.  
  65. ; check if the two cities are no more than 600 km apart
  66. (defn close [c1 c2]
  67. (let [dist (distance c1 c2)]
  68. (< dist 600))
  69. )
  70.  
  71. (println " ")
  72. (println "Are Toronto and La Ronge Close?")
  73. (println(close (city "Toronto") (city "La Ronge")))
  74. (println " ")
  75.  
  76.  
  77.  
  78. ; find distinct pairs of city names of *large* cities
  79. ; that are close.
  80. (defn closest-city-pairs []
  81. (let [y ((cities) vec)
  82. :when (large y)]
  83. (for [a (butlast y)
  84. b (rest y)
  85. :when (and (not= a b)
  86. (close a b))]
  87. [(:city a) (:city b) (distance a b)])))
  88.  
  89. (println " ")
  90. (println "List of closest city pairs")
  91. (print (closest-city-pairs))
  92. (println " ")
RAW Paste Data