Feature intersection(), union(), and difference()

Fluxxdog

Active member
From original thread HERE
Fluxxdog said:
I was wondering if Mafia had built in support for comparing aggregates, such as seeing if $items[alpha,bravo,charlie] had any matches in $items[alpha,beta,gamma]
Veracity said:
Sounds like you'd like "intersection( map, map )". And, presumably, "union( map, map )" and "difference( map, map )". Those are all "set" operations, but since an ASH map cannot have duplicate elements, they should be doable.

ASH does not have them. currently, but they should be doable. And probably reasonably "fun" to implement. Make a Feature Request.
<later>
I've thought about this, and it's a lot more complicated than I indicated.

- item arrays, as you used in your example, are arrays, not maps. Thus, they CAN have duplicates:

Code:
> ashq foreach it in $items[snorkel, disco ball, snorkel ] print( it )

snorkel
disco ball
snorkel
So, we'd have to convert the arrays into sets, perform the operation (intersection, union, set_difference), and turn the result back into an array.

- maps cannot have duplicate elements, but they have both a key and a value. We can do (intersection, union, set_difference) on the keyset - but which value gets stored in the resulting map? If I have two maps that map int to int, one has 2->3, the other has 2->4, what IS the (intersection, union, set_difference)? The intersection is probably empty, and the union and set_difference probably includes BOTH mappings - which is not possible with a map.

It's still an interesting problem, but it's not straightforward - and there's a fair amount of specification, needed, to define what is the correct and useful syntax and behavior. Perhaps it only would work on arrays - which is exactly what you were asking about, anyway.
In my original query, the example I had actually used a combination. I keep forgetting the difference between maps and arrays. One of the things I was looking to do was (briefly reading the terms) see if a map could be compared to an array:
Code:
boolean[item]x;
x[$item[alpha]]=true;
x[$item[bravo]]=true;
x[$item[charlie]]=true;
boolean[item]y=$items[alpha,beta,gamma];

>ash count(intersection(x,y))
Returned: 1
Granted I'm probably oversimplifying this, but I hope I come across clearly.

In reference to the comparison of the 2 maps returning identical keys but different values for union and set_difference, why couldn't it be returned simply as an array? Isn't that how functions that return an aggregate behave or does something like get_monsters(location) return a map?
 
Top