Last active
December 18, 2015 01:29
-
-
Save martintrojer/5704143 to your computer and use it in GitHub Desktop.
The power of dynamic languages
This file contains 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
$ lein repl | |
user=> (def res (slurp "http://www.bbc.co.uk/tv/programmes/genres/drama/scifiandfantasy/schedules/upcoming.json")) | |
user=> (require 'clojure.data.json 'clojure.walk) | |
user=> (def json (->> res clojure.data.json/read-str | |
clojure.walk/keywordize-keys)) | |
user=> (->> json :broadcasts (filter #(>= (:duration %) 6300)) | |
(map :programme) (map (juxt :title :pid))) | |
(["Lady in the Water" "b00l5wdn"] ["Lady in the Water" "b00l5wdn"] ["Lady in the Water" "b00l5wdn"] ["Lady in the Water" "b00l5wdn"]) |
This file contains 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
$ fsharpi | |
> #r "FSharp.Data.dll";; | |
> type Data = JsonProvider<"http://www.bbc.co.uk/tv/programmes/genres/drama/scifiandfantasy/schedules/upcoming.json">;; | |
type Data = JsonProvider<...> | |
> let json = Data.GetSample();; | |
val json : JsonProvider<...>.DomainTypes.Entity = | |
{JsonValue = ... } | |
> json.Broadcasts |> Seq.filter (fun m -> m.Duration >= 6300) | |
|> Seq.map (fun m -> m.Programme) | |
|> Seq.map (fun m -> [ m.Title; m.Pid ]);; | |
val it : seq<string list> = | |
seq [["b0015wdn"; "Lady in the Water"]; ["b0015wdn"; "Lady in the Water"]; ["b0015wdn"; "Lady in the Water"]] |
This file contains 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
$ scala | |
scala> val res = scala.io.Source.fromURL("http://www.bbc.co.uk/tv/programmes/genres/drama/scifiandfantasy/schedules/upcoming.json").mkString | |
res: String = Option = "{ ... }" | |
scala> import scala.util.parsing.json._ | |
scala> val json = JSON.parseFull(res) | |
json: Option[Any] = Some(Map( ... )) | |
scala> val broadcasts = json match { case Some(m: Map[String,List[Map[String,Any]]]) => m("broadcasts") } | |
// warnings... | |
broadcasts: List[Map[String,Any]] = List(Map( ... )) | |
scala> broadcasts filter (_("duration").asInstanceOf[Double] >= 6300 ) | |
map (_("programme").asInstanceOf[Map[String,Any]]) | |
map (m => (m("title"),m("pid"))) | |
res47: List[(Any, Any)] = List((Lady in the Water,b00l5wdn), (Lady in the Water,b00l5wdn), (Lady in the Water,b00l5wdn), (Lady in the Water,b00l5wdn)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment