Skip to content

Instantly share code, notes, and snippets.

@jarohen
Forked from himerzi/core.clj
Last active December 28, 2015 16:49
Show Gist options
  • Save jarohen/7531313 to your computer and use it in GitHub Desktop.
Save jarohen/7531313 to your computer and use it in GitHub Desktop.
(ns weather.core
(:require [clj-http.client :as http]))
;; utility Functionsp
(def weather-api
"http://api.openweathermap.org/data/2.5/")
(defn weather-find [something]
(let [response (http/get (str weather-api "find")
{:query-params {:q something}
:as :json})]
(:body response)))
(defn weather-forecast [loc days]
(let [response (http/get (str weather-api "forecast/daily")
{:query-params {:q loc
:cnt days
:units "metric"}
:as :json})]
(:list (:body response))))
(defn get-london []
(weather-find "London"))
(defn get-londons []
(map :sys (:list (get-london))))
;; Get coordinates of all Londons
(defn get-coords []
(map :coord (:list (weather-find "London"))))
(defn avg [v]
(/ (reduce + v) (count v)))
;; What has been the average temp in London over n past days.
(defn average-temp-n-days [n]
(avg (map #(get-in % [:temp :day])
(weather-forecast "London,UK" n))))
;; On how many of the next 10 days it will be cloudy (> 40% cloudy)
(defn clouds []
(count (filter #(> (% :clouds) 40)
(weather-forecast "London,UK" 10))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment