diff --git a/src/context.ml b/src/context.ml index 2082bcdd..10e1cee5 100644 --- a/src/context.ml +++ b/src/context.ml @@ -178,13 +178,12 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets >>= fun findlib_config -> let get_tool_using_findlib_config prog = - Option.bind findlib_config ~f:(fun conf -> - match Findlib.Config.get conf prog with - | None -> None - | Some s -> - match Filename.analyze_program_name s with - | In_path | Relative_to_current_dir -> which s - | Absolute -> Some (Path.of_filename_relative_to_initial_cwd s)) + let open Option.O in + findlib_config >>= fun conf -> + Findlib.Config.get conf prog >>= fun s -> + match Filename.analyze_program_name s with + | In_path | Relative_to_current_dir -> which s + | Absolute -> Some (Path.of_filename_relative_to_initial_cwd s) in let ocamlc = diff --git a/src/dune_file.ml b/src/dune_file.ml index 419006eb..2944d8b7 100644 --- a/src/dune_file.ml +++ b/src/dune_file.ml @@ -1144,9 +1144,8 @@ module Executables = struct let is_ok (_, candidate) = compare candidate link_mode = Eq in - match List.find ~f:is_ok simple_representations with - | Some (s, _) -> Some (Dsexp.unsafe_atom_of_string s) - | None -> None + List.find ~f:is_ok simple_representations + |> Option.map ~f:(fun (s, _) -> Dsexp.unsafe_atom_of_string s) let dgen link_mode = match simple_dgen link_mode with @@ -1287,13 +1286,12 @@ module Executables = struct in List.map2 names public_names ~f:(fun (_, name) (_, pub) -> - match pub with - | None -> None - | Some pub -> Some ({ Install_conf. - src = name ^ ext - ; dst = Some pub - })) - |> List.filter_map ~f:(fun x -> x) + Option.map pub ~f:(fun pub -> + { Install_conf. + src = name ^ ext + ; dst = Some pub + })) + |> List.filter_opt in match to_install with | [] -> begin diff --git a/src/findlib.ml b/src/findlib.ml index 9604fc23..027adaa3 100644 --- a/src/findlib.ml +++ b/src/findlib.ml @@ -304,9 +304,9 @@ let find_and_acknowledge_meta t ~fq_name = else loop dirs | [] -> - match String.Map.find t.builtins root_name with - | Some meta -> Some (t.stdlib_dir, Path.of_string "", meta) - | None -> None + String.Map.find t.builtins root_name + |> Option.map ~f:(fun meta -> + (t.stdlib_dir, Path.of_string "", meta)) in match loop t.path with | None -> diff --git a/src/installed_dune_file.ml b/src/installed_dune_file.ml index d57b74e7..a3fa496f 100644 --- a/src/installed_dune_file.ml +++ b/src/installed_dune_file.ml @@ -4,13 +4,13 @@ let parse_sub_systems ~parsing_context sexps = List.filter_map sexps ~f:(fun sexp -> let name, ver, data = Dsexp.Of_sexp.(parse (triple string (located Syntax.Version.dparse) raw) - parsing_context) sexp + parsing_context) sexp in (* We ignore sub-systems that are not internally known. These correspond to plugins that are not in use in the current workspace. *) - Option.bind (Sub_system_name.get name) ~f:(fun name -> - Some (name, (Dsexp.Ast.loc sexp, ver, data))) + Option.map (Sub_system_name.get name) ~f:(fun name -> + (name, (Dsexp.Ast.loc sexp, ver, data)))) |> Sub_system_name.Map.of_list |> (function | Ok x -> x diff --git a/src/ocaml-config/ocaml_config.ml b/src/ocaml-config/ocaml_config.ml index dd4bb5df..fc577b1c 100644 --- a/src/ocaml-config/ocaml_config.ml +++ b/src/ocaml-config/ocaml_config.ml @@ -261,13 +261,11 @@ module Vars = struct fail "Value of %S is neither 'true' neither 'false': %s." var s let get_int_opt t var = - match get_opt t var with - | None -> None - | Some s -> + Option.bind (get_opt t var) ~f:(fun s -> match int_of_string s with | x -> Some x | exception _ -> - fail "Value of %S is not an integer: %s." var s + fail "Value of %S is not an integer: %s." var s) let get_words t var = match get_opt t var with diff --git a/src/scope.ml b/src/scope.ml index 0b67d28a..6e0b9089 100644 --- a/src/scope.ml +++ b/src/scope.ml @@ -77,19 +77,17 @@ module DB = struct let public_libs = let public_libs = List.filter_map internal_libs ~f:(fun (_dir, lib) -> - match lib.public with - | None -> None - | Some p -> Some (Dune_file.Public_lib.name p, lib.project)) + Option.map lib.public ~f:(fun p -> + (Dune_file.Public_lib.name p, lib.project))) |> String.Map.of_list |> function | Ok x -> x | Error (name, _, _) -> match List.filter_map internal_libs ~f:(fun (_dir, lib) -> - match lib.public with - | None -> None - | Some p -> Option.some_if (name = Dune_file.Public_lib.name p) - lib.buildable.loc) + Option.bind lib.public ~f:(fun p -> + Option.some_if (name = Dune_file.Public_lib.name p) + lib.buildable.loc)) with | [] | [_] -> assert false | loc1 :: loc2 :: _ -> diff --git a/src/stdune/list.ml b/src/stdune/list.ml index 0c48614d..404c5fbf 100644 --- a/src/stdune/list.ml +++ b/src/stdune/list.ml @@ -14,6 +14,14 @@ let rec filter_map l ~f = | None -> filter_map l ~f | Some x -> x :: filter_map l ~f +let rec filter_opt l = + match l with + | [] -> [] + | x :: l -> + match x with + | None -> filter_opt l + | Some x -> x :: filter_opt l + let filteri l ~f = let rec filteri l i = match l with diff --git a/src/stdune/list.mli b/src/stdune/list.mli index 505b1de0..76d453d6 100644 --- a/src/stdune/list.mli +++ b/src/stdune/list.mli @@ -6,6 +6,8 @@ val is_empty : _ t -> bool val filter_map : 'a t -> f:('a -> 'b option) -> 'b t +val filter_opt : 'a option t -> 'a t + val filteri : 'a t -> f:(int -> 'a -> bool) -> 'a t val concat_map : 'a t -> f:('a -> 'b t) -> 'b t