Created
January 21, 2016 20:45
-
-
Save kopasetik/c73333a2d5bb11a246e2 to your computer and use it in GitHub Desktop.
Clojure JSON destructuring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns clojure-noob.core | |
| (:gen-class) | |
| (:require [clojure.data.json :as json] | |
| [clojure.pprint :as pp])) | |
| (defn get-url | |
| "get a url's content" | |
| [url] | |
| (-> url | |
| slurp | |
| json/read-str)) | |
| (defn isMovie? | |
| ; Boolean to determine whether it's a movie or not | |
| [item] | |
| (if (= (item "Type") "movie") | |
| true | |
| false)) | |
| (defn -main | |
| "goal: read the json contents of an API endpoint" | |
| [] | |
| (let | |
| [{movies "Search"} (get-url "http://omdbapi.com/?s=highlander")] | |
| (doall (map #(pp/pprint %) (filter isMovie? movies))))) | |
| ;=> Highlander (1986) | |
| ;=> Highlander II: The Quickening (1991) | |
| ;=> Highlander: Endgame (2000) | |
| ;=> Highlander: The Final Dimension (1994) | |
| ;=> Highlander: The Source (2007) | |
| ;=> Highlander: The Search for Vengeance (2007) | |
| ;=> Highlander: The Adventure Begins (1994) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment