0
$\begingroup$

I am trying to come up with less than and less than or equals operators for a Map where values are Sets. These are the hints I was able to get from the Professor.

{A -> {1,2}, B -> {3,4}} < {A -> {1,2}, B -> {3,4}, C -> {5,6}}

{A -> {1,2}, B -> {3,4}} < {A -> {1,2}, B -> {3,4,5}} 

My solution was this:

val v_compare = (x, y) => (x.keySet intersect y.keySet).forall(key => x(key) subsetOf y(key))

val v_compare_equal = v_compare

I am wondering can I get some hints? I don't know what should the "<=" be

$\endgroup$
1
$\begingroup$

I think that actual code is off-topic here. In general you can define a strict total order relation on (sub)sets of a known universe $U$: pick an arbitrary order for the elements of $U$ then, given two distinct subsets $X,Y$ of $U$, you can compare $X$ and $Y$ by first sorting the elements of $X$ (resp. $Y$) and then comparison the sorted version of $X$ with the sorted version of $Y$ lexicographically.

In your particular example the sets seem to contain integers, so you can just sort the set using the standard "less than" relation between integers.

To extend this total order to a map you can notice that a map is essentially a container of pairs of the form (key, value). We just defined a linear order over the values (sets), and you probably already have a natural order over the keys. Then two (key, value) pairs can be again compared lexicographically.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.