Simplify code with Option.{map,bind}
Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
parent
2cfd38c199
commit
e43dcc919d
|
@ -178,13 +178,12 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
|
||||||
>>= fun findlib_config ->
|
>>= fun findlib_config ->
|
||||||
|
|
||||||
let get_tool_using_findlib_config prog =
|
let get_tool_using_findlib_config prog =
|
||||||
Option.bind findlib_config ~f:(fun conf ->
|
let open Option.O in
|
||||||
match Findlib.Config.get conf prog with
|
findlib_config >>= fun conf ->
|
||||||
| None -> None
|
Findlib.Config.get conf prog >>= fun s ->
|
||||||
| Some s ->
|
match Filename.analyze_program_name s with
|
||||||
match Filename.analyze_program_name s with
|
| In_path | Relative_to_current_dir -> which s
|
||||||
| In_path | Relative_to_current_dir -> which s
|
| Absolute -> Some (Path.of_filename_relative_to_initial_cwd s)
|
||||||
| Absolute -> Some (Path.of_filename_relative_to_initial_cwd s))
|
|
||||||
in
|
in
|
||||||
|
|
||||||
let ocamlc =
|
let ocamlc =
|
||||||
|
|
|
@ -1144,9 +1144,8 @@ module Executables = struct
|
||||||
let is_ok (_, candidate) =
|
let is_ok (_, candidate) =
|
||||||
compare candidate link_mode = Eq
|
compare candidate link_mode = Eq
|
||||||
in
|
in
|
||||||
match List.find ~f:is_ok simple_representations with
|
List.find ~f:is_ok simple_representations
|
||||||
| Some (s, _) -> Some (Dsexp.unsafe_atom_of_string s)
|
|> Option.map ~f:(fun (s, _) -> Dsexp.unsafe_atom_of_string s)
|
||||||
| None -> None
|
|
||||||
|
|
||||||
let dgen link_mode =
|
let dgen link_mode =
|
||||||
match simple_dgen link_mode with
|
match simple_dgen link_mode with
|
||||||
|
@ -1287,13 +1286,12 @@ module Executables = struct
|
||||||
in
|
in
|
||||||
List.map2 names public_names
|
List.map2 names public_names
|
||||||
~f:(fun (_, name) (_, pub) ->
|
~f:(fun (_, name) (_, pub) ->
|
||||||
match pub with
|
Option.map pub ~f:(fun pub ->
|
||||||
| None -> None
|
{ Install_conf.
|
||||||
| Some pub -> Some ({ Install_conf.
|
src = name ^ ext
|
||||||
src = name ^ ext
|
; dst = Some pub
|
||||||
; dst = Some pub
|
}))
|
||||||
}))
|
|> List.filter_opt
|
||||||
|> List.filter_map ~f:(fun x -> x)
|
|
||||||
in
|
in
|
||||||
match to_install with
|
match to_install with
|
||||||
| [] -> begin
|
| [] -> begin
|
||||||
|
|
|
@ -304,9 +304,9 @@ let find_and_acknowledge_meta t ~fq_name =
|
||||||
else
|
else
|
||||||
loop dirs
|
loop dirs
|
||||||
| [] ->
|
| [] ->
|
||||||
match String.Map.find t.builtins root_name with
|
String.Map.find t.builtins root_name
|
||||||
| Some meta -> Some (t.stdlib_dir, Path.of_string "<internal>", meta)
|
|> Option.map ~f:(fun meta ->
|
||||||
| None -> None
|
(t.stdlib_dir, Path.of_string "<internal>", meta))
|
||||||
in
|
in
|
||||||
match loop t.path with
|
match loop t.path with
|
||||||
| None ->
|
| None ->
|
||||||
|
|
|
@ -4,13 +4,13 @@ let parse_sub_systems ~parsing_context sexps =
|
||||||
List.filter_map sexps ~f:(fun sexp ->
|
List.filter_map sexps ~f:(fun sexp ->
|
||||||
let name, ver, data =
|
let name, ver, data =
|
||||||
Dsexp.Of_sexp.(parse (triple string (located Syntax.Version.dparse) raw)
|
Dsexp.Of_sexp.(parse (triple string (located Syntax.Version.dparse) raw)
|
||||||
parsing_context) sexp
|
parsing_context) sexp
|
||||||
in
|
in
|
||||||
(* We ignore sub-systems that are not internally known. These
|
(* We ignore sub-systems that are not internally known. These
|
||||||
correspond to plugins that are not in use in the current
|
correspond to plugins that are not in use in the current
|
||||||
workspace. *)
|
workspace. *)
|
||||||
Option.bind (Sub_system_name.get name) ~f:(fun name ->
|
Option.map (Sub_system_name.get name) ~f:(fun name ->
|
||||||
Some (name, (Dsexp.Ast.loc sexp, ver, data)))
|
(name, (Dsexp.Ast.loc sexp, ver, data))))
|
||||||
|> Sub_system_name.Map.of_list
|
|> Sub_system_name.Map.of_list
|
||||||
|> (function
|
|> (function
|
||||||
| Ok x -> x
|
| Ok x -> x
|
||||||
|
|
|
@ -261,13 +261,11 @@ module Vars = struct
|
||||||
fail "Value of %S is neither 'true' neither 'false': %s." var s
|
fail "Value of %S is neither 'true' neither 'false': %s." var s
|
||||||
|
|
||||||
let get_int_opt t var =
|
let get_int_opt t var =
|
||||||
match get_opt t var with
|
Option.bind (get_opt t var) ~f:(fun s ->
|
||||||
| None -> None
|
|
||||||
| Some s ->
|
|
||||||
match int_of_string s with
|
match int_of_string s with
|
||||||
| x -> Some x
|
| x -> Some x
|
||||||
| exception _ ->
|
| 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 =
|
let get_words t var =
|
||||||
match get_opt t var with
|
match get_opt t var with
|
||||||
|
|
12
src/scope.ml
12
src/scope.ml
|
@ -77,19 +77,17 @@ module DB = struct
|
||||||
let public_libs =
|
let public_libs =
|
||||||
let public_libs =
|
let public_libs =
|
||||||
List.filter_map internal_libs ~f:(fun (_dir, lib) ->
|
List.filter_map internal_libs ~f:(fun (_dir, lib) ->
|
||||||
match lib.public with
|
Option.map lib.public ~f:(fun p ->
|
||||||
| None -> None
|
(Dune_file.Public_lib.name p, lib.project)))
|
||||||
| Some p -> Some (Dune_file.Public_lib.name p, lib.project))
|
|
||||||
|> String.Map.of_list
|
|> String.Map.of_list
|
||||||
|> function
|
|> function
|
||||||
| Ok x -> x
|
| Ok x -> x
|
||||||
| Error (name, _, _) ->
|
| Error (name, _, _) ->
|
||||||
match
|
match
|
||||||
List.filter_map internal_libs ~f:(fun (_dir, lib) ->
|
List.filter_map internal_libs ~f:(fun (_dir, lib) ->
|
||||||
match lib.public with
|
Option.bind lib.public ~f:(fun p ->
|
||||||
| None -> None
|
Option.some_if (name = Dune_file.Public_lib.name p)
|
||||||
| Some p -> Option.some_if (name = Dune_file.Public_lib.name p)
|
lib.buildable.loc))
|
||||||
lib.buildable.loc)
|
|
||||||
with
|
with
|
||||||
| [] | [_] -> assert false
|
| [] | [_] -> assert false
|
||||||
| loc1 :: loc2 :: _ ->
|
| loc1 :: loc2 :: _ ->
|
||||||
|
|
|
@ -14,6 +14,14 @@ let rec filter_map l ~f =
|
||||||
| None -> filter_map l ~f
|
| None -> filter_map l ~f
|
||||||
| Some x -> x :: 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 filteri l ~f =
|
||||||
let rec filteri l i =
|
let rec filteri l i =
|
||||||
match l with
|
match l with
|
||||||
|
|
|
@ -6,6 +6,8 @@ val is_empty : _ t -> bool
|
||||||
|
|
||||||
val filter_map : 'a t -> f:('a -> 'b option) -> 'b t
|
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 filteri : 'a t -> f:(int -> 'a -> bool) -> 'a t
|
||||||
|
|
||||||
val concat_map : 'a t -> f:('a -> 'b t) -> 'b t
|
val concat_map : 'a t -> f:('a -> 'b t) -> 'b t
|
||||||
|
|
Loading…
Reference in New Issue