From 22cdc884803f93565ab3e21d3f327bba4de825ad Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sat, 28 Mar 2015 11:22:25 +0600 Subject: 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"). --- src/vytree.ml | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src') 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 -- cgit v1.2.3