[groovy-dev] Implementation of map1 + map2

Previous Topic
 
classic Classic list List threaded Threaded
17 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[groovy-dev] Implementation of map1 + map2

phkim
Hi

list1 + list2 is implemented.
But map1 + map2 is not implemented yet.

I hope to execute the code:

def map1 = [one: 1, two: 2]
def map2 = [three: 3]
def map3 = map1 + map2

How about this implementation?

Kim

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: [groovy-dev] Implementation of map1 + map2

Dierk König
I generally like the idea.

what if

def map1 = [one: 1, two: 2]
def map2 = [two: 3]

assert map1 + map2 == map2 + map1

-> it's hard to make + symmetrical...

(other current + impls may have the same
problem)

cheers
Mittie

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-dev] Implementation of map1 + map2

phkim
On Sat, Oct 29, 2005 at 06:16:21PM +0200, Dierk Koenig wrote:

> I generally like the idea.
>
> what if
>
> def map1 = [one: 1, two: 2]
> def map2 = [two: 3]
>
> assert map1 + map2 == map2 + map1
>
> -> it's hard to make + symmetrical...
>
> (other current + impls may have the same
> problem)
>
> cheers
> Mittie
>

I mean only the sum of two maps.
But string + map1 + map2 means (string + map1) + map 2,
and so will be different from string + (map1 + map2).

As I know, the comaprison: map1 == map2
is implemented already.


Here is a test:

Lets get Groovy!
================
Version: 1.0-jsr-04-SNAPSHOT JVM: 1.5.0_04-b05
Type 'exit' to terminate the shell
Type 'help' for command help
Type 'go' to execute the statements

groovy> assert ['one':1, 'two':2] == [two:2, one:1]
groovy> go

groovy> def x = ['one':1, 'two':2]
groovy> def y = [two: 2]
groovy> assert x != y
groovy> go

groovy> x.putAll(['three':3])
groovy> y.putAll([three:3, one:1])
groovy> assert x == y
groovy> go

groovy> println x
groovy> println y
groovy> go
{one=1, two=2, three=3}
{one=1, two=2, three=3}

groovy> x.putAll(one:1.0)
groovy> println x
groovy> println y
groovy> go
{one=1.0, two=2, three=3}
{one=1, two=2, three=3}


Kim

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: [groovy-dev] Implementation of map1 + map2

Dierk König
sure. my concern was (in terms of putAll as +)

a = [one: 1, two: 2]
b = [two: 3]
 
a.putAll([two: 3])
b.putAll([one: 1, two: 2])
assert a == b

which fails

cheers
Mittie

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> Sent: Samstag, 29. Oktober 2005 17:38
> To: [hidden email]
> Subject: Re: [groovy-dev] Implementation of map1 + map2
>
>
> On Sat, Oct 29, 2005 at 06:16:21PM +0200, Dierk Koenig wrote:
> > I generally like the idea.
> >
> > what if
> >
> > def map1 = [one: 1, two: 2]
> > def map2 = [two: 3]
> >
> > assert map1 + map2 == map2 + map1
> >
> > -> it's hard to make + symmetrical...
> >
> > (other current + impls may have the same
> > problem)
> >
> > cheers
> > Mittie
> >
>
> I mean only the sum of two maps.
> But string + map1 + map2 means (string + map1) + map 2,
> and so will be different from string + (map1 + map2).
>
> As I know, the comaprison: map1 == map2
> is implemented already.
>
>
> Here is a test:
>
> Lets get Groovy!
> ================
> Version: 1.0-jsr-04-SNAPSHOT JVM: 1.5.0_04-b05
> Type 'exit' to terminate the shell
> Type 'help' for command help
> Type 'go' to execute the statements
>
> groovy> assert ['one':1, 'two':2] == [two:2, one:1]
> groovy> go
>
> groovy> def x = ['one':1, 'two':2]
> groovy> def y = [two: 2]
> groovy> assert x != y
> groovy> go
>
> groovy> x.putAll(['three':3])
> groovy> y.putAll([three:3, one:1])
> groovy> assert x == y
> groovy> go
>
> groovy> println x
> groovy> println y
> groovy> go
> {one=1, two=2, three=3}
> {one=1, two=2, three=3}
>
> groovy> x.putAll(one:1.0)
> groovy> println x
> groovy> println y
> groovy> go
> {one=1.0, two=2, three=3}
> {one=1, two=2, three=3}
>
>
> Kim
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-dev] Implementation of map1 + map2

phkim
On Sat, Oct 29, 2005 at 07:16:22PM +0200, Dierk Koenig wrote:

> sure. my concern was (in terms of putAll as +)
>
> a = [one: 1, two: 2]
> b = [two: 3]
>  
> a.putAll([two: 3])
> b.putAll([one: 1, two: 2])
> assert a == b
>
> which fails
>
> cheers
> Mittie
>

Of course.

a = "AB"
b = "C"
assert a + b != b + a

The commutative law of string1 + string2 does not hold.
Similarly, that of map1 + map2 doesn't need hold.


Kim

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: [groovy-dev] Implementation of map1 + map2

Dierk König
> a = "AB"
> b = "C"
> assert a + b != b + a
>
> The commutative law of string1 + string2 does not hold.
> Similarly, that of map1 + map2 doesn't need hold.

We actually have sequence-aware classes like
Strings, Lists, etc. where + means 'append'.
(+ does not need to be symmetrical)

For classes that are not sequence-aware like
Sets and Maps where + should mean 'union'.
Unions follow the axiom of symmetry: a+b==b+a

We had a similar discussion for - meaning
'remove' or 'difference'.

However, I would like to have some consistency.
Maybe
 + meaning add
 - meaning remove
 | meaning union        // symmetrical
 & meaning intersection // symmetrical
 ^ meaning difference   // symmetrical

So
 map1+map2
works in your proposed way while
 map1 | map2
throws an IllegalArgumentException if
 // keys overlap with different values
 map1.keySet().any{
     map2.keySet().contains(it) && map1[it]!=map2[it]
 }

cheers
Mittie


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-dev] Implementation of map1 + map2

phkim
On Sat, Oct 29, 2005 at 08:01:08PM +0200, Dierk Koenig wrote:

> We actually have sequence-aware classes like
> Strings, Lists, etc. where + means 'append'.
> (+ does not need to be symmetrical)
>
> For classes that are not sequence-aware like
> Sets and Maps where + should mean 'union'.
> Unions follow the axiom of symmetry: a+b==b+a
>
> We had a similar discussion for - meaning
> 'remove' or 'difference'.
>
> However, I would like to have some consistency.
> Maybe
>  + meaning add
>  - meaning remove

The implementation of map1 - map2 is a good idea.

>  | meaning union        // symmetrical
>  & meaning intersection // symmetrical
>  ^ meaning difference   // symmetrical
>

But
   map1 | map2
   map1 & map2
   map1 ^ map2
are ambiguous operations because maps have values.


Kim

> So
>  map1+map2
> works in your proposed way while
>  map1 | map2
> throws an IllegalArgumentException if
>  // keys overlap with different values
>  map1.keySet().any{
>      map2.keySet().contains(it) && map1[it]!=map2[it]
>  }
>
> cheers
> Mittie
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-dev] Implementation of map1 + map2

phkim
On Sun, Oct 30, 2005 at 02:43:25AM +0900, [hidden email] wrote:

> On Sat, Oct 29, 2005 at 08:01:08PM +0200, Dierk Koenig wrote:
> > We actually have sequence-aware classes like
> > Strings, Lists, etc. where + means 'append'.
> > (+ does not need to be symmetrical)
> >
> > For classes that are not sequence-aware like
> > Sets and Maps where + should mean 'union'.
> > Unions follow the axiom of symmetry: a+b==b+a
> >
> > We had a similar discussion for - meaning
> > 'remove' or 'difference'.
> >
> > However, I would like to have some consistency.
> > Maybe
> >  + meaning add
> >  - meaning remove
>
> The implementation of map1 - map2 is a good idea.

What is the result of [one:1, two:2] - [one:3] ?

[one:1, two:2] ?  or [two:2] ?


Kim

>
> >  | meaning union        // symmetrical
> >  & meaning intersection // symmetrical
> >  ^ meaning difference   // symmetrical
> >
>
> But
>    map1 | map2
>    map1 & map2
>    map1 ^ map2
> are ambiguous operations because maps have values.
>
>
> Kim
>
> > So
> >  map1+map2
> > works in your proposed way while
> >  map1 | map2
> > throws an IllegalArgumentException if
> >  // keys overlap with different values
> >  map1.keySet().any{
> >      map2.keySet().contains(it) && map1[it]!=map2[it]
> >  }
> >
> > cheers
> > Mittie
> >
> >
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-dev] Implementation of map1 + map2

Jochen Theodorou
In reply to this post by Dierk König
Dierk Koenig schrieb:

[...]
> However, I would like to have some consistency.
> Maybe
>  + meaning add

you forgot << which is ment to append, so + adds an element:

[a:1] + [b:2] --> [a:1,[b:2]]
[a:1] << [b:2] --> [a:1,b:2]

bye blackdrag
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-dev] Implementation of map1 + map2

phkim
On Sat, Oct 29, 2005 at 08:22:50PM +0200, Jochen Theodorou wrote:

> Dierk Koenig schrieb:
>
> [...]
> >However, I would like to have some consistency.
> >Maybe
> > + meaning add
>
> you forgot << which is ment to append, so + adds an element:
>
> [a:1] + [b:2] --> [a:1,[b:2]]

[a:1] + [b:2] --> [a:1,[b:2]]  // this is not a map, isn't it?

assert [a:1] + [b:2] == [a:1, *:[b:2]]

where *: is the spread operator for map.


> [a:1] << [b:2] --> [a:1,b:2]
>
> bye blackdrag


12
Loading...