Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
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>&nbsp; [data]
<br>&nbsp; (doseq [area data
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; warehouse (:warehouses area)
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container (:containers warehouse)
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; box (:boxes container)]
<br>&nbsp; &nbsp; (if-not (empty? (:items box))
<br>&nbsp; &nbsp; &nbsp; (doseq [item (:items box)] (do-something (:box box) (:item item)))
<br>&nbsp; &nbsp; &nbsp; (do-something-else (:warehouse warehouse)
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(:container container)
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(:box box)))))
<br>
<br>Might be more appropriate...
<br>
<br>
<br>On Mon, Oct 1, 2012 at 5:07 PM, arekanderu &lt;<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3Yu3_7ju40oJ">areka...@gmail.com</a>&gt; wrote:
<br>&gt;&gt; (def my-data [{:area "Somewhere" :warehouses
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[{:warehouse "W54321" :containers
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [{:container "C12345" :boxes
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [{:box "B12345" :items
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [{:item "I12345"}]}]}]}]}
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {:area "SomewhereElse" :warehouses
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[{:warehouse "W54321" :containers
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [{:container "C54321" :boxes
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [{:box "B54321" :items
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [{:item "I54321"}]}]}]}]}])
<br>&gt;&gt;
<br>&gt;&gt;
<br>&gt;&gt; (defn my-func [data]
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; (map (fn [area]
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(map (fn [warehouse]
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (map (fn [container]
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(map (fn [box]
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (if (not (empty? (:items box)))
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (map (fn [item]
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(doSomething (:box box) (:item
<br>&gt;&gt; item)))
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(:items box))
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (doSomethingElse (:warehouse warehouse)
<br>&gt;&gt; (:container container) (:box box))))
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (:boxes container)))
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(:containers warehouse)))
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (:warehouses area)))
<br>&gt;&gt;
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;data))
<br></blockquote></div>
------=_Part_766_33222769.1349130975010--

------=_Part_765_22266451.1349130975010--