I wrote a much better example in order to show what am I trying to do. Of
course, the following is a simplified version of the actual code in order
to make things easier for everybody.
(def my-data [{:area "Somewhere" :warehouses
>
> [{:warehouse "W54321" :containers
>
> [{:container "C12345" :boxes
>
> [{:box "B12345" :items
>
> [{:item "I12345"}]}]}]}]}
>
> {:area "SomewhereElse" :warehouses
>
> [{:warehouse "W54321" :containers
>
> [{:container "C54321" :boxes
>
> [{:box "B54321" :items
>
> [{:item "I54321"}]}]}]}]}])
>
>
>> (defn my-func [data]
>
> (map (fn [area]
>
> (map (fn [warehouse]
>
> (map (fn [container]
>
> (map (fn [box]
>
> (if (not (empty? (:items box)))
>
> (map (fn [item]
>
> (doSomething (:box box) (:item
>> item)))
>
> (:items box))
>
> (doSomethingElse (:warehouse warehouse)
>> (:container container) (:box box))))
>
> (:boxes container)))
>
> (:containers warehouse)))
>
> (:warehouses area)))
>
> data))
>
>
My question is, how can I get rid of the nested loops and replace it with
something more elegant in clojure.
Thank you for any replies.
On Monday, October 1, 2012 1:16:25 AM UTC+3, arekanderu wrote:
>
> Thank you for your prompt reply Grant.
>
> *> May you share the original code? *
> *
> *
> I will post the original function very soon*
> *
>
> *> Why does my-map have vectors storing maps inside instead of a map with
> > maps inside? *
> *
> *
> Because each vector will have more than one hash-map and each hash map
> will have multiple keys. I only posted a simplified version of the map for
> readability, unless I am missing something here...
> I think of vector as the equivalent of the "array of" and that's why I
> used it. If i should have done it in some other way please let me know.
>
> *> May you write some tests to demonstrate what you want to accomplish
> > with the ugly map and share them here? *
> *
> *
> I am basically trying to destruct my deep map with a better way than a
> 4-nested loop.
>
> I will post a better code snippet in order to clean things even more.
>
> Thanks again
>
> On Monday, October 1, 2012 12:41:52 AM UTC+3, Grant Rettke wrote:
>>
>> On Sun, Sep 30, 2012 at 2:59 PM, arekanderu <[email protected]> wrote:
>> > I am trying to port an ugly piece of code from Ruby to clojure.
>>
>> May you share the original code?
>>
>> > So far I
>> > have only ported it to clojure by keeping the same way it was written
>> in
>> > Ruby and i am trying to re-write it the clojure way
>> because...well....its
>> > very ugly.
>>
>> Why does my-map have vectors storing maps inside instead of a map with
>> maps inside?
>>
>> > I have a complex hash map which it's structure is always the same and
>> the
>> > keys always known and by using the values in those keys, i make some
>> > function calls with those values as parameters. You can find a
>> simplified
>> > version of the map and the function which steps into the map below.
>>
>> May you write some tests to demonstrate what you want to accomplish
>> with the ugly map and share them here?
>>
>> > I would really appreciate it if someone could propose an alternative
>> way
>> > of writing the above function or at least to point me where can I look
>> for
>> > some useful clojure functions that will help me do what I want but in a
>> > cleaner way.
>>
>> Once you provide tests then we have a better chance at helping.
>>
>> --
>> ((λ (x) (x x)) (λ (x) (x x)))
>> http://www.wisdomandwonder.com/
>> ACM, AMA, COG, IEEE
>>
>
--
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