diff options
author | ianhin <ianhin> | 2006-02-23 03:16:26 +0000 |
---|---|---|
committer | ianhin <ianhin> | 2006-02-23 03:16:26 +0000 |
commit | 13995ec275315d08fbd2d8c9f5922692487ddd8c (patch) | |
tree | b3326e95029f72cc0a76570cd196c7d21f692133 /Tools/MathematicaMisc | |
parent | 4c8fa0a57c89a705a1cd5e6722e9672bde3e248e (diff) |
Added error checking on most of the lookup functions. Checks that the
map passed is in fact a list of rule objects.
Diffstat (limited to 'Tools/MathematicaMisc')
-rw-r--r-- | Tools/MathematicaMisc/MapLookup.m | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/Tools/MathematicaMisc/MapLookup.m b/Tools/MathematicaMisc/MapLookup.m index 6d355ba..faa553c 100644 --- a/Tools/MathematicaMisc/MapLookup.m +++ b/Tools/MathematicaMisc/MapLookup.m @@ -17,10 +17,19 @@ Begin["`Private`"]; Map lookup -------------------------------------------------------------------------- *) +VerifyRule[r_] := + If[! Head[r] === Rule, + ThrowError["Expecting a rule, but found", r]]; + +VerifyMap[m_] := + Module[{}, + If[!ListQ[m], + ThrowError["Expecting a map (list of rules) but found", m]]; + Map[VerifyRule, m]]; + lookup[map_, key_] := Module[{values}, - If[! ListQ[map], - ThrowError["lookup failure: map", map, " is not a list"]]; + VerifyMap[map]; values = Select[map, First[#] === key &]; If[values == {}, @@ -31,23 +40,33 @@ lookup[map_, key_] := First[values][[2]]]; mapContains[map_, key_] := - Length[Select[map, First[#] === key &]] >= 1; + Module[{}, + VerifyMap[map]; + Length[Select[map, First[#] === key &]] >= 1]; lookupDefault[map_, key_, default_] := - If[mapContains[map, key], - lookup[map, key], - default]; + Module[{}, + VerifyMap[map]; + If[mapContains[map, key], + lookup[map, key], + default]]; mapReplace[map_, key_, value_] := - Map[If[First[#] === key, key -> value, #] &, map]; + Module[{}, + VerifyMap[map]; + Map[If[First[#] === key, key -> value, #] &, map]]; mapAdd[map_, key_, value_] := - Join[map, {key -> value}]; + Module[{}, + VerifyMap[map]; + Join[map, {key -> value}]]; mapEnsureKey[map_, key_, defaultValue_] := + Module[{}, + VerifyMap[map]; If[mapContains[map, key], map, - mapAdd[map, key, defaultValue]]; + mapAdd[map, key, defaultValue]]]; mapValueMap[map_, key_, f_] := If[mapContains[map, key], |