Message from discussion
Transforming an ugly nested loop into clojure code
Received: by 10.52.70.78 with SMTP id k14mr4223114vdu.7.1349130982490;
Mon, 01 Oct 2012 15:36:22 -0700 (PDT)
X-BeenThere: clojure@googlegroups.com
Received: by 10.220.38.68 with SMTP id a4ls2388800vce.6.gmail; Mon, 01 Oct
2012 15:36:15 -0700 (PDT)
Received: by 10.52.93.132 with SMTP id cu4mr1680010vdb.14.1349130975278;
Mon, 01 Oct 2012 15:36:15 -0700 (PDT)
Date: Mon, 1 Oct 2012 15:36:14 -0700 (PDT)
From: arekanderu <arekand...@gmail.com>
To: clojure@googlegroups.com
Message-Id: <9a9946d4-91d6-4d35-9923-c074b4b8473e@googlegroups.com>
In-Reply-To: <CAPdvrr8BOyysxgWF8EWxTQcvA79zQt2HQ-a5uXsYjsLLjKjSuQ@mail.gmail.com>
References: <e12b012d-0d71-4af8-843f-103334d1d343@googlegroups.com>
<CAAjq1mcg0MVFe7CMHG6O6RPPcriw8wOAVwt0PSgvAaU50M3nng@mail.gmail.com>
<9fb065cf-7e3d-4c16-9830-d74ad00ed386@googlegroups.com>
<f69e830c-0297-4c88-b535-6adb145a8915@googlegroups.com>
<CAPdvrr8BOyysxgWF8EWxTQcvA79zQt2HQ-a5uXsYjsLLjKjSuQ@mail.gmail.com>
Subject: Re: Transforming an ugly nested loop into clojure code
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_765_22266451.1349130975010"
------=_Part_765_22266451.1349130975010
Content-Type: multipart/alternative;
boundary="----=_Part_766_33222769.1349130975010"
------=_Part_766_33222769.1349130975010
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Thank you Gaz for your reply. You are correct about the side effects.
I will go through your example more carefully and give it a go. It
definitely looks more promising than what I got :)
On Tuesday, October 2, 2012 1:22:44 AM UTC+3, Gaz wrote:
>
> You appear to be running over the map purely for side-effects,
> therefore off the top of my head something like:
>
> (defn my-func
> [data]
> (doseq [area data
> warehouse (:warehouses area)
> container (:containers warehouse)
> box (:boxes container)]
> (if-not (empty? (:items box))
> (doseq [item (:items box)] (do-something (:box box) (:item item)))
> (do-something-else (:warehouse warehouse)
> (:container container)
> (:box box)))))
>
> Might be more appropriate...
>
>
> On Mon, Oct 1, 2012 at 5:07 PM, arekanderu <areka...@gmail.com<javascript:>>
> wrote:
> >> (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))
>
------=_Part_766_33222769.1349130975010
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
Thank you Gaz for your reply. You are correct about the side effects.<div><br></div><div>I will go through your example more carefully and give it a go. It definitely looks more promising than what I got :)<br><br>On Tuesday, October 2, 2012 1:22:44 AM UTC+3, Gaz wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">You appear to be running over the map purely for side-effects,
<br>therefore off the top of my head something like:
<br>
<br>(defn my-func
<br> [data]
<br> (doseq [area data
<br> warehouse (:warehouses area)
<br> container (:containers warehouse)
<br> box (:boxes container)]
<br> (if-not (empty? (:items box))
<br> (doseq [item (:items box)] (do-something (:box box) (:item item)))
<br> (do-something-else (:warehouse warehouse)
<br> (:container container)
<br> (:box box)))))
<br>
<br>Might be more appropriate...
<br>
<br>
<br>On Mon, Oct 1, 2012 at 5:07 PM, arekanderu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3Yu3_7ju40oJ">areka...@gmail.com</a>> wrote:
<br>>> (def my-data [{:area "Somewhere" :warehouses
<br>>>
<br>>> [{:warehouse "W54321" :containers
<br>>>
<br>>> [{:container "C12345" :boxes
<br>>>
<br>>> [{:box "B12345" :items
<br>>>
<br>>> [{:item "I12345"}]}]}]}]}
<br>>>
<br>>> {:area "SomewhereElse" :warehouses
<br>>>
<br>>> [{:warehouse "W54321" :containers
<br>>>
<br>>> [{:container "C54321" :boxes
<br>>>
<br>>> [{:box "B54321" :items
<br>>>
<br>>> [{:item "I54321"}]}]}]}]}])
<br>>>
<br>>>
<br>>> (defn my-func [data]
<br>>>
<br>>> (map (fn [area]
<br>>>
<br>>> (map (fn [warehouse]
<br>>>
<br>>> (map (fn [container]
<br>>>
<br>>> (map (fn [box]
<br>>>
<br>>> (if (not (empty? (:items box)))
<br>>>
<br>>> (map (fn [item]
<br>>>
<br>>> (doSomething (:box box) (:item
<br>>> item)))
<br>>>
<br>>> (:items box))
<br>>>
<br>>> (doSomethingElse (:warehouse warehouse)
<br>>> (:container container) (:box box))))
<br>>>
<br>>> (:boxes container)))
<br>>>
<br>>> (:containers warehouse)))
<br>>>
<br>>> (:warehouses area)))
<br>>>
<br>>> data))
<br></blockquote></div>
------=_Part_766_33222769.1349130975010--
------=_Part_765_22266451.1349130975010--