From fc4e68c00cbe8fca004f08867fec6eaaf0178383 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 4 Mar 2015 23:12:09 +0600 Subject: Use an exception-throwing version of find instead of repeating the matching everywhere. --- src/vytree.ml | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'src') 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 -- cgit v1.2.3