PreviousUpNext

Red-Black Trees: Folding

We might want to combine all the values in ascending or descending order. Here the "" arguments to fold_left and fold_right are the initial values for the result accumulators:

    linux$ my

    eval:  include package   string_map;

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

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

    eval:  fold_forward  (\\ (value,result) = sprintf "%s,%s" value result) "" m;
    "Value3,Value2,Value1,"

    eval:  fold_backward  (\\ (value,result) = sprintf "%s,%s" value result) "" m;
    "Value1,Value2,Value3,"

Or the same as the above but with the keys added into the mix:

    linux$ my

    eval:  include package   string_map;

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

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

    eval:  keyed_fold_forward  (\\ (key,value,result) = sprintf "(%s,%s),%s" key value result) "" m;
    "(Key3,Value3),(Key2,Value2),(Key1,Value1),"

    eval:  keyed_fold_backward (\\ (key,value,result) = sprintf "(%s,%s),%s" key value result) "" m;
    "(Key1,Value1),(Key2,Value2),(Key3,Value3),"

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext