You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.5 KiB
95 lines
2.5 KiB
5 years ago
|
(*
|
||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*)
|
||
|
|
||
5 years ago
|
open NS0
|
||
5 years ago
|
include Core.List
|
||
5 years ago
|
|
||
|
let rec pp ?pre ?suf sep pp_elt fs = function
|
||
|
| [] -> ()
|
||
|
| x :: xs ->
|
||
|
Option.iter pre ~f:(Format.fprintf fs) ;
|
||
|
pp_elt fs x ;
|
||
|
( match xs with
|
||
|
| [] -> ()
|
||
|
| xs -> Format.fprintf fs "%( %)%a" sep (pp sep pp_elt) xs ) ;
|
||
|
Option.iter suf ~f:(Format.fprintf fs)
|
||
|
|
||
5 years ago
|
let findi x xs =
|
||
|
let rec findi_ i xs =
|
||
|
match xs with
|
||
|
| [] -> None
|
||
|
| x' :: _ when x == x' -> Some i
|
||
|
| _ :: xs -> findi_ (i + 1) xs
|
||
|
in
|
||
|
findi_ 0 xs
|
||
|
|
||
5 years ago
|
let pop_exn = function
|
||
|
| x :: xs -> (x, xs)
|
||
|
| [] -> raise (Not_found_s (Atom __LOC__))
|
||
5 years ago
|
|
||
|
let find_map_remove xs ~f =
|
||
|
let rec find_map_remove_ ys = function
|
||
|
| [] -> None
|
||
|
| x :: xs -> (
|
||
|
match f x with
|
||
|
| Some x' -> Some (x', rev_append ys xs)
|
||
|
| None -> find_map_remove_ (x :: ys) xs )
|
||
|
in
|
||
|
find_map_remove_ [] xs
|
||
|
|
||
|
let fold_option xs ~init ~f =
|
||
5 years ago
|
let@ {return} = with_return in
|
||
5 years ago
|
Some
|
||
|
(fold xs ~init ~f:(fun acc elt ->
|
||
|
match f acc elt with Some res -> res | None -> return None ))
|
||
|
|
||
5 years ago
|
let map_endo t ~f = map_endo map t ~f
|
||
5 years ago
|
|
||
|
let rev_map_unzip xs ~f =
|
||
|
fold xs ~init:([], []) ~f:(fun (ys, zs) x ->
|
||
|
let y, z = f x in
|
||
|
(y :: ys, z :: zs) )
|
||
|
|
||
5 years ago
|
let remove_exn ?(equal = phys_equal) xs x =
|
||
|
let rec remove_ ys = function
|
||
|
| [] -> raise (Not_found_s (Atom __LOC__))
|
||
|
| z :: xs ->
|
||
|
if equal x z then rev_append ys xs else remove_ (z :: ys) xs
|
||
|
in
|
||
|
remove_ [] xs
|
||
5 years ago
|
|
||
|
let remove ?equal xs x =
|
||
5 years ago
|
try Some (remove_exn ?equal xs x) with Not_found_s _ -> None
|
||
5 years ago
|
|
||
|
let rec rev_init n ~f =
|
||
|
if n = 0 then []
|
||
|
else
|
||
|
let n = n - 1 in
|
||
|
let xs = rev_init n ~f in
|
||
|
f n :: xs
|
||
|
|
||
|
let symmetric_diff ~compare xs ys =
|
||
5 years ago
|
let rec symmetric_diff_ xxs yys : _ Either.t list =
|
||
5 years ago
|
match (xxs, yys) with
|
||
|
| x :: xs, y :: ys ->
|
||
|
let ord = compare x y in
|
||
|
if ord = 0 then symmetric_diff_ xs ys
|
||
5 years ago
|
else if ord < 0 then First x :: symmetric_diff_ xs yys
|
||
|
else Second y :: symmetric_diff_ xxs ys
|
||
|
| xs, [] -> map ~f:Either.first xs
|
||
|
| [], ys -> map ~f:Either.second ys
|
||
5 years ago
|
in
|
||
|
symmetric_diff_ (sort ~compare xs) (sort ~compare ys)
|
||
|
|
||
|
let pp_diff ~compare sep pp_elt fs (xs, ys) =
|
||
|
let pp_diff_elt fs elt =
|
||
5 years ago
|
match elt with
|
||
5 years ago
|
| First x -> Format.fprintf fs "-- %a" pp_elt x
|
||
|
| Second y -> Format.fprintf fs "++ %a" pp_elt y
|
||
5 years ago
|
in
|
||
|
pp sep pp_diff_elt fs (symmetric_diff ~compare xs ys)
|