diff options
author | Daniil Baturin <daniil@baturin.org> | 2015-03-28 11:22:25 +0600 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2015-03-28 11:22:25 +0600 |
commit | 22cdc884803f93565ab3e21d3f327bba4de825ad (patch) | |
tree | 183bf8e50597940652b24166f2df99b2dc93f305 /src | |
parent | 8fdefc9d6c7c76cb295b1c5b42a371d2394238a6 (diff) | |
download | vyconf-22cdc884803f93565ab3e21d3f327bba4de825ad.tar.gz vyconf-22cdc884803f93565ab3e21d3f327bba4de825ad.zip |
Abstract the "find a node and do things to it" away a little.
insert remains a special case, maybe it can fit it in it if we pass
to functions instead of one (one for "found" and another one for "not found").
Diffstat (limited to 'src')
-rw-r--r-- | src/vytree.ml | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/vytree.ml b/src/vytree.ml index 8c64fc6..de482ce 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -8,6 +8,8 @@ type 'a t = 'a vyconf_tree type node_type = Tag | Leaf | Other +type position = Before of string | After of string | Default + exception Empty_path exception Duplicate_child exception Nonexistent_path @@ -50,6 +52,15 @@ let find_or_fail node name = let list_children node = List.map (fun x -> x.name) node.children +let rec do_with_child fn node path = + match path with + | [] -> raise Empty_path + | [name] -> fn node name + | name :: names -> + let next_child = find_or_fail node name in + let new_node = do_with_child fn next_child names in + replace node new_node + let rec insert default_data node path data = match path with | [] -> raise Empty_path @@ -72,14 +83,8 @@ let rec insert default_data node path data = let new_node = insert default_data next_child' names data in adopt node new_node -let rec delete node path = - match path with - | [] -> raise Empty_path - | [name] -> delete_immediate node name - | name :: names -> - let next_child = find_or_fail node name in - let new_node = delete next_child names in - replace node new_node +let delete node path = + do_with_child delete_immediate node path let rec get node path = match path with |