Skip to content

Commit

Permalink
another implementation for BatList.cartesian_product
Browse files Browse the repository at this point in the history
this implementation avoids the call to List.concat

as explained in issue ocaml-batteries-team#997
  • Loading branch information
fccm committed Dec 29, 2020
1 parent b106a58 commit 457e2ec
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/batList.mlv
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,14 @@ let group cmp lst =
*)

let cartesian_product l1 l2 =
List.concat (List.map (fun i -> List.map (fun j -> (i,j)) l2) l1)
let l1 = List.rev l1 in
let l2 = List.rev l2 in
List.fold_left
(fun acc a ->
List.fold_left
(fun acc b -> (a, b)::acc)
acc l2)
[] l1

(*$T cartesian_product as cp
cp [1;2;3] ['x';'y'] = [1,'x';1,'y';2,'x';2,'y';3,'x';3,'y']
Expand Down

0 comments on commit 457e2ec

Please sign in to comment.