PreviousUpNext

Red-Black Trees: Union and Intersection

We might want to take the union or intersection of two maps, with an adjudication function to decide which value to use when the same key is present in both maps:

    linux$ my

    eval:  include package   string_map;

    eval:  m1 = (empty: Map( String ));

    eval:  m1 $= ("Key1", "Value1");
    eval:  m1 $= ("Key2", "Value2");
    eval:  m1 $= ("Key3", "Value3");

    eval:  m2 = (empty: Map( String ));

    eval:  m2 $= ("Key0", "Value0");
    eval:  m2 $= ("Key2", "value2");
    eval:  m2 $= ("Key4", "Value4");

    eval:  m3 = union_with  (\\ (value1, value2) = value1 + value2) (m1, m2);

    eval:  keyvals_list m3;

    [("Key0", "Value0"),
     ("Key1", "Value1"), 
     ("Key2", "Value2value2"),
     ("Key3", "Value3"), 
     ("Key4", "Value4")]

    eval:  m4 = intersect_with  (\\ (value1, value2) = value1 + value2) (m1, m2);

    eval:  keyvals_list m4;

    [("Key2", "Value2value2")]

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext