Skip to content

Commit c6eda1a

Browse files
committed
cli_operations: Refactor chained maps and filters
make_param_funs was building up a list by applying List.map and List.filter several times. This commit combines and streamlines these. Signed-off-by: Christian Pardillo Laursen <[email protected]>
1 parent ff34a8a commit c6eda1a

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

ocaml/xapi-cli-server/cli_operations.ml

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -550,20 +550,18 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
550550
)
551551
all
552552
in
553-
(* Filter on everything on the cmd line except params=... *)
554553
let filter_params =
555-
List.filter
556-
(fun (p, _) -> not (List.mem p ("params" :: stdparams)))
557-
params
558-
in
559-
(* Filter out all params beginning with "database:" *)
560-
let filter_params =
561-
List.filter
562-
(fun (p, _) -> not (Astring.String.is_prefix ~affix:"database:" p))
563-
filter_params
554+
(* Add in the default filters *)
555+
def_filters
556+
@ List.filter
557+
(fun (p, _) ->
558+
(* Filter on everything on the cmd line except params=... *)
559+
(not (List.mem p ("params" :: stdparams)))
560+
(* Filter out all params beginning with "database:" *)
561+
&& not (Astring.String.is_prefix ~affix:"database:" p)
562+
)
563+
params
564564
in
565-
(* Add in the default filters *)
566-
let filter_params = def_filters @ filter_params in
567565
(* Filter all the records *)
568566
let records =
569567
List.fold_left filter_records_on_fields all_recs filter_params
@@ -573,22 +571,16 @@ let make_param_funs getallrecs getbyuuid record class_name def_filters
573571
select_fields params
574572
(if print_all then all_recs else records)
575573
def_list_params
576-
in
577-
let print_params =
578-
List.map
579-
(fun fields -> List.filter (fun field -> not field.hidden) fields)
580-
print_params
581-
in
582-
let print_params =
583-
List.map
584-
(fun fields ->
585-
List.map
586-
(fun field ->
587-
if field.expensive then makeexpensivefield field else field
588-
)
589-
fields
590-
)
591-
print_params
574+
|> List.map (fun fields ->
575+
fields
576+
|> List.filter (fun field -> not field.hidden)
577+
|> List.map (fun field ->
578+
if field.expensive then
579+
makeexpensivefield field
580+
else
581+
field
582+
)
583+
)
592584
in
593585
printer
594586
(Cli_printer.PTable (List.map (List.map print_field) print_params))

0 commit comments

Comments
 (0)