diff options
author | Daniil Baturin <daniil@baturin.org> | 2015-03-04 23:12:09 +0600 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2015-03-04 23:12:09 +0600 |
commit | fc4e68c00cbe8fca004f08867fec6eaaf0178383 (patch) | |
tree | 2e24fe782edf0e687f6b154c52114e6c6a34f421 /src | |
parent | 6d6b9e94100a0ceeb72c7b030f9ae871087f6889 (diff) | |
download | vyconf-fc4e68c00cbe8fca004f08867fec6eaaf0178383.tar.gz vyconf-fc4e68c00cbe8fca004f08867fec6eaaf0178383.zip |
Use an exception-throwing version of find instead of repeating the matching everywhere.
Diffstat (limited to 'src')
-rw-r--r-- | src/vytree.ml | 31 |
1 files changed, 11 insertions, 20 deletions
diff --git a/src/vytree.ml b/src/vytree.ml index 689cf01..2a05d3d 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -39,6 +39,12 @@ let replace node child = let find node name = Vylist.find (fun x -> x.name = name) node.children +let find_or_fail node name = + let child = find node name in + match child with + | None -> raise Nonexistent_path + | Some child' -> child' + let rec extract_names children = List.map (fun x -> x.name) children @@ -72,27 +78,12 @@ let rec delete node path = | [] -> raise Empty_path | [name] -> delete_immediate node name | name :: names -> - let next_child = find node name in - match next_child with - | None -> raise Nonexistent_path - | Some next_child' -> - let new_node = delete next_child' names in - replace node new_node + let next_child = find_or_fail node name in + let new_node = delete next_child names in + replace node new_node let rec get node path = match path with | [] -> raise Empty_path - | [name] -> - begin - let child = find node name in - match child with - | None -> raise Nonexistent_path - | Some child' -> child' - end - | name :: names -> - begin - let next_child = find node name in - match next_child with - | None -> raise Nonexistent_path - | Some child' -> get child' names - end + | [name] -> find_or_fail node name + | name :: names -> get (find_or_fail node name) names |