Simplify code with Option.{map,bind}

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-08-25 13:44:00 +03:00
parent 2cfd38c199
commit e43dcc919d
8 changed files with 37 additions and 34 deletions

View File

@ -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 =

View File

@ -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

View File

@ -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 "<internal>", meta)
| None -> None
String.Map.find t.builtins root_name
|> Option.map ~f:(fun meta ->
(t.stdlib_dir, Path.of_string "<internal>", meta))
in
match loop t.path with
| None ->

View File

@ -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

View File

@ -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

View File

@ -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 :: _ ->

View File

@ -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

View File

@ -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