Skip to content

Commit b03d87d

Browse files
authored
Replace List.fold_left (||) false (List.map f lst) with List.exists f lst (#6590)
It's an equivalent, but much cleaner construct, which in addition avoids needlessly iterating over the list after the first match is found.
2 parents 11e9e2b + f721629 commit b03d87d

File tree

10 files changed

+31
-45
lines changed

10 files changed

+31
-45
lines changed

ocaml/rrd2csv/src/rrd2csv.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ module Ds_selector = struct
304304
if fs = [] then
305305
true
306306
else
307-
List.fold_left (fun acc f -> acc || filter11 f d) false fs
307+
List.exists (fun f -> filter11 f d) fs
308308

309309
(* Returns the d \in ds that passes at least one of the filters
310310
fs *)

ocaml/tests/binpack_test.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ let check_plan config dead_hosts plan =
4545
let memory_remaining = account config.hosts config.vms plan in
4646
(* List.iter (fun mem -> Printf.printf "%Ld\n" mem) free; *)
4747
(* No host should be overcommitted: *)
48-
if
49-
List.fold_left ( || ) false
50-
(List.map (fun x -> x < 0L) (List.map snd memory_remaining))
51-
then
48+
if List.exists (fun (_, x) -> x < 0L) memory_remaining then
5249
raise BadPlan ;
5350
(* All failed VMs should be restarted: *)
5451
let failed_vms = get_failed_vms config dead_hosts in

ocaml/xapi-cli-server/cli_util.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ let track callback rpc (session_id : API.ref_session) task =
9191
| _ ->
9292
false
9393
in
94-
finished := List.fold_left ( || ) false (List.map matches events)
94+
finished := List.exists matches events
9595
done
9696
with
9797
| Api_errors.Server_error (code, _)

ocaml/xapi-idl/storage/storage_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let names =
6363

6464
let vdi_exists sr vdi =
6565
let all = Client.SR.scan dbg sr in
66-
List.fold_left (fun acc vdi_info -> acc || vdi_info.vdi = vdi) false all
66+
List.exists (fun vdi_info -> vdi_info.vdi = vdi) all
6767

6868
let create sr name_label =
6969
let vdi_info =

ocaml/xapi-idl/storage/vdi_automaton.ml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,9 @@ let ( + ) state operation =
9494

9595
let superstate states =
9696
let activated =
97-
List.fold_left
98-
(fun acc s -> acc || s = Activated RO || s = Activated RW)
99-
false states
100-
in
101-
let rw =
102-
List.fold_left
103-
(fun acc s -> acc || s = Activated RW || s = Attached RW)
104-
false states
97+
List.exists (fun s -> s = Activated RO || s = Activated RW) states
10598
in
99+
let rw = List.exists (fun s -> s = Activated RW || s = Attached RW) states in
106100
if states = [] then
107101
Detached
108102
else if activated then

ocaml/xapi/helpers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ let pool_has_different_host_platform_versions ~__context =
10121012
let is_different_to_me platform_version =
10131013
platform_version <> Xapi_version.platform_version ()
10141014
in
1015-
List.fold_left ( || ) false (List.map is_different_to_me platform_versions)
1015+
List.exists is_different_to_me platform_versions
10161016

10171017
(* Checks that a host has a PBD for a particular SR (meaning that the
10181018
SR is visible to the host) *)

ocaml/xapi/pool_features_helpers.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,16 @@ let rec compute_additional_restrictions all_host_params = function
5858
[]
5959
| flag :: rest ->
6060
let switches =
61-
List.map
61+
List.exists
6262
(function
6363
| params ->
64-
if List.mem_assoc flag params then
65-
bool_of_string (List.assoc flag params)
66-
else
67-
true
64+
List.assoc_opt flag params
65+
|> Fun.flip Option.bind bool_of_string_opt
66+
|> Option.value ~default:true
6867
)
6968
all_host_params
7069
in
71-
(flag, string_of_bool (List.fold_left ( || ) false switches))
70+
(flag, string_of_bool switches)
7271
:: compute_additional_restrictions all_host_params rest
7372

7473
(* Combine the host-level feature restrictions into pool-level ones, and write

ocaml/xapi/xapi_bond.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,9 @@ let create ~__context ~network ~members ~mAC ~mode ~properties =
427427
in
428428
let disallow_unplug =
429429
(* this is always true if one of the PIFs is a cluster_host.PIF *)
430-
List.fold_left
431-
(fun a m -> Db.PIF.get_disallow_unplug ~__context ~self:m || a)
432-
false members
430+
List.exists
431+
(fun m -> Db.PIF.get_disallow_unplug ~__context ~self:m)
432+
members
433433
in
434434
(* Validate constraints: *)
435435
(* 1. Members must not be in a bond already *)

ocaml/xapi/xapi_globs.ml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,18 +1334,14 @@ let gen_list_option name desc of_string string_of opt =
13341334
let sm_plugins = ref []
13351335

13361336
let accept_sm_plugin name =
1337-
List.(
1338-
fold_left ( || ) false
1339-
(map
1340-
(function
1341-
| `All ->
1342-
true
1343-
| `Sm x ->
1344-
String.lowercase_ascii x = String.lowercase_ascii name
1345-
)
1346-
!sm_plugins
1337+
List.exists
1338+
(function
1339+
| `All ->
1340+
true
1341+
| `Sm x ->
1342+
String.lowercase_ascii x = String.lowercase_ascii name
13471343
)
1348-
)
1344+
!sm_plugins
13491345

13501346
let nvidia_multi_vgpu_enabled_driver_versions =
13511347
ref ["430.42"; "430.62"; "440.00+"]

ocaml/xapi/xapi_host.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,19 +2185,19 @@ let reset_networking ~__context ~host =
21852185
(Db.PIF.get_all ~__context)
21862186
in
21872187
let bond_is_local bond =
2188-
List.fold_left
2189-
(fun a pif -> Db.Bond.get_master ~__context ~self:bond = pif || a)
2190-
false local_pifs
2188+
List.exists
2189+
(fun pif -> Db.Bond.get_master ~__context ~self:bond = pif)
2190+
local_pifs
21912191
in
21922192
let vlan_is_local vlan =
2193-
List.fold_left
2194-
(fun a pif -> Db.VLAN.get_untagged_PIF ~__context ~self:vlan = pif || a)
2195-
false local_pifs
2193+
List.exists
2194+
(fun pif -> Db.VLAN.get_untagged_PIF ~__context ~self:vlan = pif)
2195+
local_pifs
21962196
in
21972197
let tunnel_is_local tunnel =
2198-
List.fold_left
2199-
(fun a pif -> Db.Tunnel.get_access_PIF ~__context ~self:tunnel = pif || a)
2200-
false local_pifs
2198+
List.exists
2199+
(fun pif -> Db.Tunnel.get_access_PIF ~__context ~self:tunnel = pif)
2200+
local_pifs
22012201
in
22022202
let bonds = List.filter bond_is_local (Db.Bond.get_all ~__context) in
22032203
List.iter

0 commit comments

Comments
 (0)