Skip to content

Instantly share code, notes, and snippets.

@lbdl
Last active December 28, 2015 17:39
Show Gist options
  • Save lbdl/7537354 to your computer and use it in GitHub Desktop.
Save lbdl/7537354 to your computer and use it in GitHub Desktop.
clojure experiments for clojure school
(ns weather.core
(:require [clj-http.client :as http]))
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
(def weather-api
"http://api.openweathermap.org/data/2.5/")
(defn weather-find [location]
(http/get (str weather-api "find")
{:query-params {:q location}
:as :json}))
(defn forecast-find [location number-of-days]
(http/get (str weather-api "forecast/daily")
{:query-params {:q location
:cnt number-of-days
:units "metric" }
:as :json}))
(defn number-of [town-names]
(:count (:body (weather-find town-names))))
(defn country [town-name]
(list "GB" (:list (:body (forecast-find town-name)))))
(defn lat-long [town-name]
(map :coord (:list (:body (weather-find town-name)))))
(defn temperatures [town-name]
(map :temp (map :main (:list (:body (weather-find town-name))))))
(defn sum [town-name]
(reduce +(temperatures town-name)))
(defn avg [town-name]
(/ (reduce + (temperatures town-name)) (number-of town-name)))
(defn avg [v]
(/ (reduce + v) (count v)))
(defn average-temp-over-days [town-name number-of-days]
(avg (map #(get-in % [:temp :day]) (:list (:body (forecast-find town-name number-of-days))))))
(defn cloudy-in-town-for-number-days [town-name number-of-days]
(count (filter #(> (% :clouds) 40)
(:list(:body(forecast-find town-name number-of-days))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment