Thanks, superficial changes are good too :)

----

One question I forgot that is important is:

- how would I accomplish the 'maybe' monad style where the web service call 
would return nil? on failure.  Is the normal pattern for this an if-let at 
the top before the rest of the transformations are applied?

On Tuesday, April 23, 2013 5:19:08 PM UTC-4, Steven Degutis wrote:
>
> I'm also quite new to Clojure, but here are some very superficial changes 
> I would make to your code, without actually giving much thought to your 
> algorithms:
>
> (defn- transform-album-images [images]
>   (into {} (for [image images]
>              [(:size image)
>               (:#text image)])))
>  
> (defn- transform-album [album]
>   (-> (select-keys album [:name :artist :image])
>     (update-in [:image] transform-album-images)
>     (clojure.set/rename-keys { :image :images })))
>  
> (defn search [term]
>   (let [albums (-> (api/get {:method "album.search" :album term})
>                  (get-in [:results :albummatches :album] [])
>                  (vector))]
>     {:albums (map transform-album albums)}))
>
> -Steven
>
>
> On Tue, Apr 23, 2013 at 4:12 PM, Huey Petersen <eys...@gmail.com<javascript:>
> > 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 clo...@googlegroups.com<javascript:>
>> Note that posts from new members are moderated - please be patient with 
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+u...@googlegroups.com <javascript:>
>> 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 clojure+u...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>  
>>  
>>
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to