Raynes recently released https://github.com/Raynes/least, a Clojure client
for the last.fm API. Have you looked into just using his, rather than
reinventing the API layer yourself?
On Tuesday, April 23, 2013 2:12:37 PM UTC-7, Huey Petersen wrote:
>
> Howdy,
>
> I'm a clojure fan but quite new to writing clojure. I'm writing my first
> app and had a few style questions.
>
> I'm doing a web service call to the lastfm api. It returns some json like:
>
> {
> "results": {
> "albummatches": {
> "album": [
> {
> "name": "In The Future",
> "artist": "Black Mountain",
> "image": [{
> "size": "small",
> "#text": "http://example.com/image/url.jpg"
> }]
> }
> ]
> }
> }
> }
>
>
> One notes is that the 'album' key in the json can either be missing (0
> results) a map itself (1 result) or the array (2+ results).
>
> I then want to turn it into structure like:
>
> {
> "albums": [{
> "name": "In The Future",
> "artist": "Black Mountain",
> "images": {
> "small": "http://example.com/image/url.jpg"
> }
> }]
> }
>
>
> Here is the clojure code I came up with:
>
> (defn- transform-album-images [images]
> (into {} (map (fn [image] [(:size image) (:#text image)]) images)))
>
> (defn- transform-album [album]
> (let [album (select-keys album [:name :artist :image])
> album (update-in album [:image] transform-album-images)
> album (clojure.set/rename-keys album { :image :images })]
> album))
>
> (defn search [term]
> (let [albums (api/get {:method "album.search" :album term})
> albums (get-in albums [:results :albummatches :album] [])
> albums (if (vector? albums) albums (vector albums))
> albums (map transform-album albums)]
> {:albums albums}))
>
>
>
> My main question is whether this is at all 'idomatic' clojure. I have a
> few specific questions too.
>
> - I broke out two functions -- transform-album-images and transform-album
> -- because I wasn't sure how to squeeze them into the first function. They
> really have no purpose outside of the 'search' function though. I don't
> know why, but it kinda bugs me because it seems to detract from the main
> function 'search' as they have to appear above 'search'.
>
> - the (api/get ...) call talks to an external webservice. Everything else
> is a 'pure' function. Is including the (api/get) call inside the 'search'
> function bad? I'm a bit hazy on the write split from pure / non-pure code.
>
> I feel like transforming data between two representations with a side
> effect at each end is like 90% of code I write so I'd like to get a good
> feel for doing this in clojure :)
>
> Thanks for any tips.
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.