[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-user
Subject:    Re: [groovy-user] Deep Add on Collections
From:       Dinko Srkoč <dinko.srkoc () gmail ! com>
Date:       2013-09-30 21:31:51
Message-ID: CAFPyv-wUdQtgx56=z8f=gTEzH9kB0Knh8JmU3nJ_iiER7gKiuA () mail ! gmail ! com
[Download RAW message or body]

I too am wondering what would be the obvious approach here. Hopefully not
this one: ;-)

def deepAdd(Map m1, Map m2) {
    m2.inject(m1 ?: [:]) { acc, k2, v2 ->
        acc + [(k2): deepAdd(acc[k2], v2)]
    }
}
def deepAdd(List l1, List l2) {
    (l1 ?: []) + l2
}
def deepAdd(o1, o2) { o2 }

Cheers,
Dinko


On 30 September 2013 20:57, Jochen Theodorou <blackdrag@gmx.org> wrote:

> Am 30.09.2013 18:49, schrieb Steve Amerige:
>
>> I'm wondering anyone has implemented a *deepAdd *to add two collections:
>>
>>
>>      def m1 = [a: [x:1, z:3], c:[5,7]]
>>      def m2 = [a: [x:2, y:4], c:[6]]
>>
>>      def sum = deepAdd(m1, m2)
>>      assert sum == [a:[x:2, z:3, y:4], c:[5,7,6]]
>>
>> The deepAdd method should operate at any depth. One would need to deal
>> with what happens when corresponding entries are not compatible (such as
>> either choosing to throw an exception or have the new value supersede
>> the old value).
>>
>
> Not sure what you mean with "not compatible". I would assume that if the
> key is the same (for hashmap), that we have that case. In your example you
> have that with a.x:1 and a.x:2 and suggested the old value to be not
> visible anymore. I think that is reasonable.
>
>
>  Any thoughts as to how you'd further refine this problem and write a
>> reasonably general solution for objects that are built up from maps and
>> arrays?  The solution is very straightforward if I just do the 'obvious'
>> things. I'm just wondering if there are any Groovy shortcuts that make
>> the solution simpler than the obvious plodding approach.
>>
>
> I suggest you show your solution so far and maybe we can suggest
> improvements. Or what non-obvious cases do you have in mind here?
>
>
> bye blackdrag
>
> --
> Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
> blog: http://blackdragsview.**blogspot.com/<http://blackdragsview.blogspot.com/>
> german groovy discussion newsgroup: de.comp.lang.misc
> For Groovy programming sources visit http://groovy-lang.org
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/**manage_email<http://xircles.codehaus.org/manage_email>
>
>
>

[Attachment #3 (text/html)]

<div dir="ltr"><div class="markdown-here-wrapper" id="markdown-here-wrapper-766893" \
style><p style="margin:1.2em 0px!important">I too am wondering what would be the \
obvious approach here. Hopefully not this one: ;-)</p> <pre \
style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em \
0px"><code class="language-groovy" \
style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px \
0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid \
rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-t \
op-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;displ \
ay:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px \
solid rgb(204,204,204);padding:0.5em \
0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial \
initial"><span class="function"><span class="keyword" \
style="color:rgb(51,51,51);font-weight:bold">def</span> <span class="title" \
style="color:rgb(153,0,0);font-weight:bold">deepAdd</span><span class="params">(<span \
class="constant">Map</span> m1, <span class="constant">Map</span> m2)</span> </span>{ \
m2.inject(m1 ?<span class="symbol" style="color:rgb(153,0,115)">:</span> [<span \
                class="symbol" style="color:rgb(153,0,115)">:</span>]) { acc, k2, v2 \
                -&gt;
        acc + [(k2)<span class="symbol" style="color:rgb(153,0,115)">:</span> \
deepAdd(acc[k2], v2)]  }
}

<span class="function"><span class="keyword" \
style="color:rgb(51,51,51);font-weight:bold">def</span> <span class="title" \
style="color:rgb(153,0,0);font-weight:bold">deepAdd</span><span class="params">(<span \
class="constant">List</span> l1, <span class="constant">List</span> l2)</span> \
</span>{  (l1 ?<span class="symbol" style="color:rgb(153,0,115)">:</span> []) + l2
}

<span class="function"><span class="keyword" \
style="color:rgb(51,51,51);font-weight:bold">def</span> <span class="title" \
style="color:rgb(153,0,0);font-weight:bold">deepAdd</span><span class="params">(o1, \
o2)</span> </span>{ o2 }</code></pre>


<p style="margin:1.2em 0px!important">Cheers,<br>Dinko</p>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 30 September \
2013 20:57, Jochen Theodorou <span dir="ltr">&lt;<a href="mailto:blackdrag@gmx.org" \
target="_blank">blackdrag@gmx.org</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">Am 30.09.2013 18:49, schrieb Steve Amerige:<br> <blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"> I&#39;m wondering anyone has implemented a *deepAdd *to add \
two collections:<div class="im"><br> <br>
        def m1 = [a: [x:1, z:3], c:[5,7]]<br>
        def m2 = [a: [x:2, y:4], c:[6]]<br>
<br>
        def sum = deepAdd(m1, m2)<br>
        assert sum == [a:[x:2, z:3, y:4], c:[5,7,6]]<br>
<br>
The deepAdd method should operate at any depth. One would need to deal<br>
with what happens when corresponding entries are not compatible (such as<br>
either choosing to throw an exception or have the new value supersede<br>
the old value).<br>
</div></blockquote>
<br>
Not sure what you mean with &quot;not compatible&quot;. I would assume that if the \
key is the same (for hashmap), that we have that case. In your example you have that \
with a.x:1 and a.x:2 and suggested the old value to be not visible anymore. I think \
that is reasonable.<div class="im">

<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"> Any thoughts as to how you&#39;d further refine this problem \
and write a<br> reasonably general solution for objects that are built up from maps \
and<br> arrays?   The solution is very straightforward if I just do the \
&#39;obvious&#39;<br> things. I&#39;m just wondering if there are any Groovy \
shortcuts that make<br> the solution simpler than the obvious plodding approach.<br>
</blockquote>
<br></div>
I suggest you show your solution so far and maybe we can suggest improvements. Or \
what non-obvious cases do you have in mind here?<div class="HOEnZb"><div \
class="h5"><br> <br>
bye blackdrag<br>
<br>
-- <br>
Jochen &quot;blackdrag&quot; Theodorou - Groovy Project Tech Lead<br>
blog: <a href="http://blackdragsview.blogspot.com/" \
target="_blank">http://blackdragsview.<u></u>blogspot.com/</a><br> german groovy \
discussion newsgroup: de.comp.lang.misc<br> For Groovy programming sources visit <a \
href="http://groovy-lang.org" target="_blank">http://groovy-lang.org</a><br> <br>
<br>
------------------------------<u></u>------------------------------<u></u>---------<br>
 To unsubscribe from this list, please visit:<br>
<br>
     <a href="http://xircles.codehaus.org/manage_email" \
target="_blank">http://xircles.codehaus.org/<u></u>manage_email</a><br> <br>
<br>
</div></div></blockquote></div><br></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic