Skip to content

Instantly share code, notes, and snippets.

@himerzi
Last active December 28, 2015 16:39

Revisions

  1. himerzi revised this gist Nov 18, 2013. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions core.clj
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@
    ;; This is broken and I don't understand why
    (defn get-londons []
    (map #(:sys %) (:list get-london)))
    ;; This works, and I don't understand why
    ;; Get's name and location of all londons. This works, and I don't understand why
    (defn get-londons-2 []
    (map #(:sys %) (:list (weather-find "London"))))
    ;; Get coordinates of all Londons
    @@ -35,5 +35,4 @@

    ;;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))))

    (count(filter #(> (% :clouds) 40) (weather-forecast "London,UK" 10))))
  2. himerzi created this gist Nov 18, 2013.
    39 changes: 39 additions & 0 deletions core.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    (ns weather.core
    (:require [clj-http.client :as http]))
    ;; utility Functions
    (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"))
    ;; This is broken and I don't understand why
    (defn get-londons []
    (map #(:sys %) (:list get-london)))
    ;; This works, and I don't understand why
    (defn get-londons-2 []
    (map #(:sys %) (:list (weather-find "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))))