package b0

  1. Overview
  2. Docs
Software construction and deployment kit

Install

dune-project
 Dependency

Authors

Maintainers

Sources

b0-0.0.6.tbz
sha512=e9aa779e66c08fc763019f16d4706f465d16c05d6400b58fbd0313317ef33ddea51952e2b058db28e65f7ddb7012f328c8bf02d8f1da17bb543348541a2587f0

doc/src/b0.std/b0__list.ml.html

Source file b0__list.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(*---------------------------------------------------------------------------
   Copyright (c) 2025 The more programmers. All rights reserved.
   SPDX-License-Identifier: ISC
  ---------------------------------------------------------------------------*)

include List

let cons_if b v vs = if b then v :: vs else vs

let classify
    (type a) (type b)
    ?(cmp_elts : a -> a -> int = Stdlib.compare)
    ?(cmp_classes : b -> b -> int = Stdlib.compare)
    ~classes:(classes : (a -> b list)) els
  =
  let module Set = Set.Make (struct type t = a let compare = cmp_elts end) in
  let module Map = Map.Make (struct type t = b let compare = cmp_classes end) in
  let add_classes acc p =
    let add_class acc c = try Map.add c (Set.add p (Map.find c acc)) acc with
    | Not_found -> Map.add c (Set.singleton p) acc
    in
    List.fold_left add_class acc (classes p)
  in
  let classes = List.fold_left add_classes Map.empty els in
  List.rev (Map.fold (fun c els acc -> (c, Set.elements els) :: acc) classes [])

let distinct (type a) (compare : a -> a -> int) vs =
  let module Set = Set.Make (struct type t = a let compare = compare end) in
  let rec loop seen acc = function
  | [] -> List.rev acc
  | v :: vs ->
      if Set.mem v seen then loop seen acc vs else
      loop (Set.add v seen) (v :: acc) vs
  in
  loop Set.empty [] vs

let rec fold_stop_on_error f l acc = match l with
| [] -> Ok acc
| v :: vs ->
    match f v acc with
    | Ok acc -> fold_stop_on_error f vs acc
    | Error _ as e -> e

let rec iter_stop_on_error f = function
| [] -> Ok ()
| v :: vs ->
    match f v with
    | Error _ as e -> e
    | Ok v -> iter_stop_on_error f vs

let rec iter_iter_on_error ~error f = function
| [] -> ()
| v :: vs ->
    (match f v with Error _ as e -> error e | Ok _ -> ());
    iter_iter_on_error ~error f vs