Yes, it's almost always a good thing to separate the formatting of data
from processing its side effects.
When talking to a database, there might already be record and
list-of-records interface, for step 2.

2013/3/25 Ryan <arekand...@gmail.com>

> Thanks a lot Leif :) It's really good to have a second opinion on these
> matter since I am new to clojure.
>
> On Monday, March 25, 2013 3:37:47 AM UTC+2, Leif wrote:
>>
>> I'm totally in favor of your alternate approach, for these reasons:
>>
>> 1.  As you noted, it's easier to test.
>> 2.  I separates your data processing from the side-effects, which lets
>> you implement batching, switch to another DB, etc. without touching the
>> data processing code.
>>
>> So I would trust your instincts on this one. :)
>>
>> --Leif
>>
>> On Sunday, March 24, 2013 8:24:53 AM UTC-4, Ryan wrote:
>>>
>>> Hello all,
>>>
>>> I am trying to figure out which is the most idiomatic way to go in a
>>> project I am doing. I've noticed the following pattern in my code and I
>>> started wondering if there is a more clojurish approach to go with it. For
>>> the shake of readability and simplicity, I will provide a pseudo-code that
>>> represents the actual flow of the code piece I wanna show.
>>>
>>> So, to get thing started, this is the pseudo-code:
>>>
>>> (defn process-bar-item [bar-item]
>>>>   (let [sub-items (. bar-item getSubItems)]
>>>>     (doseq [a-sub-item sub-items]
>>>>       ;make an update to database
>>>>       )))
>>>>
>>>
>>>
>>> (defn my-function [a-list another-list]
>>>>   (doseq [item a-list
>>>>           :let[foo    (. item getFoo)
>>>>                foo-id (. foo getID)]]
>>>>     ;make an update to database
>>>>     ;make a second update to database
>>>>     ;make a third update to database
>>>>  )
>>>>
>>>
>>>
>>> (doseq [item another-list
>>>>          :let[foo      (. item getFoo)
>>>>               foo-id   (. foo getID)
>>>>               bar-list (. foo getBars)]]
>>>>   ;make an update to database
>>>>   (doseq [bar-item bar-list]
>>>>    (process-bar-item bar-item))))
>>>
>>>
>>> Just a quick note here, process-bar-item is more complex than that, I
>>> just made it simpler so I can make this example.
>>>
>>> So, what I was wondering is this. Would it be better to:
>>>
>>>    1. Create a list/hash-map with the use of reduce which will include all
>>>    the values that I need to construct each query.
>>>    2. Pass that list to a separate function, loop that list and make
>>>    the side-effects (the database queries in my case)
>>>
>>> Does the above approach sounds better than what I am doing? Is there
>>> a preferred way to do things like this? or what I am already doing is just
>>> fine (even though I believe it will be more difficult to test it) ?
>>>
>>> Thank you for your time
>>>
>>  --
> --
> 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.


Reply via email to