1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
open Core
let unique_value xs ~equal ~f =
match xs with
| [] -> Error `Empty
| h :: t ->
let v = f h in
let mismatches =
List.filter_map t ~f:(fun x ->
let w = f x in
if equal v w then None else Some w
)
in
if List.is_empty mismatches then Ok v
else Error (`Not_unique (v :: mismatches))
let unique_string xs ~f =
match unique_value xs ~equal:String.equal ~f with
| Error `Empty -> Or_error.error_string "Empty list"
| Error (`Not_unique mismatches) ->
Or_error.error "multiple values" mismatches [%sexp_of: string list]
| Ok r -> Ok r