On Monday, May 21, 2012 6:54:28 PM UTC+3, Christian Guimaraes wrote: > > Hi all, > > I'm struggling with "when" code structures and my imperative mindset. > > What is the better approach (or functional approach) to work with a code > like the below? > > (defn parse-group [group-identifier line] > (when (= group-identifier "ID1") > (handle-id1 line)) > (when (= group-identifier "ID2") > (handle-id2 line)) > (when (= group-identifier "ID3") > (handle-id3 line))) > > Thank you. > -- christian > > >
Hi Maybe something like (defn parse-group [group-identifier line] (condp = group-identifier "ID1" (handle-id1 line) "ID2" (handle-id2 line) "ID3" (handle-id3 line))) or, if you want to leverage the fact that maps are functions: (defn parse-group [group-identifier line] (({"ID1" handle-id1 "ID2" handle-id2 "ID3" handle-id3} group-identifier) line)) but then you lose some readability IMO. On Monday, May 21, 2012 6:54:28 PM UTC+3, Christian Guimaraes wrote: > > Hi all, > > I'm struggling with "when" code structures and my imperative mindset. > > What is the better approach (or functional approach) to work with a code > like the below? > > (defn parse-group [group-identifier line] > (when (= group-identifier "ID1") > (handle-id1 line)) > (when (= group-identifier "ID2") > (handle-id2 line)) > (when (= group-identifier "ID3") > (handle-id3 line))) > > Thank you. > -- christian > > > -- 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