package ppx_yojson

  1. Overview
  2. Docs

Source file utils.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let remove ~idx l =
  let rec aux ~left ~right ~i =
    match right with
    | _::tl when i = idx -> List.rev_append left tl
    | hd::tl -> aux ~left:(hd::left) ~right:tl ~i:(i + 1)
    | [] -> l
  in
  aux ~left:[] ~right:l ~i:0

let rec permutations = function
  | [] -> [[]]
  | [elm] -> [[elm]]
  | l ->
    List.mapi
      (fun idx elm -> List.map (List.cons elm) (permutations @@ remove ~idx l))
      l
  |> List.flatten
OCaml

Innovation. Community. Security.