I agree with using the trush operator since that certainly is a pipeline. A few comments though:
(fn [image] [(:size image) (:#text image)]) Can be changed to: (juxt :size :#text) I wouldn't do api/get in search, the function is much easier to test if you keep it pure. Setup unittests with example data. Get every test working, and then just feed it live-data and voila. No ban from last-fm for spamming their servers ;) On Tue, Apr 23, 2013 at 11:19 PM, Steven Degutis <sbdegu...@gmail.com>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> 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 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. >> >> >> > > -- > -- > 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. > > > -- -- 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.