package stdlib-diff

All checked builds pass
  1. Overview
  2. Docs
Symmetric Diffs for OCaml stdlib and ReasonML

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.1.3.tar.gz
md5=dd714e1969c6b28a143843c046970c0f
sha512=3df3b4ab6f3043fcc0486f494d47ebbfded9e8929e998f2d2435cf05090b4bd318e096bf55590826df1b5d2036c473c28f4529242044430c3e0b44072b68a5f2

Description

Added to opam-repository:

README

Symmetric Diffs for OCaml Standard Library

ReasonML implementation of fast symmetric diffs for Sets and Maps. Library is designed to be used together with OCaml standard library that is shipped with BuckleScript.

Algorithm was inspired by Jane Street's Base library (that unfortunately cannot be used in BuckleScript directly).

Installation

Install the package using npm:

npm install ocaml-diff

Add dependency to your bsconfig.json:

"bs-dependencies": [
  "ocaml-diff"
],

Usage

For more examples see examples folder.

Note: Diffing function is combined together with folding/reducing functionality to allow convenient direct accumulation of changes.

Set

Following example for string set accumulates all added values into list:

module StringSet = OcamlDiff.Set.Make(String);

let oldSet = StringSet.(empty |> add("A") |> add("B") |> add("C"));
let newSet =
  StringSet.(oldSet |> remove("A") |> remove("C") |> add("D") |> add("C"));
let addedItems =
  StringSet.symmetric_diff(oldSet, newSet, ~acc=[], ~f=(either, acc) =>
    switch (either) {
    | Left(a) =>
      Js.log("Element " ++ a ++ " is only in the old set.");
      acc;
    | Right(a) =>
      Js.log("Element " ++ a ++ " is only in the new set.");
      [a, ...acc];
    }
  );
// Element A is only in the old set.
// Element D is only in the new set.

Js.log(Array.of_list(addedItems));
// [ 'D' ]

Map

Argument ~veq specifies function for detecting equal values with signature ('value, 'value) => bool.

Following example for string map accumulates all added values:

module StringMap = OcamlDiff.Map.Make(String);

let oldMap = StringMap.(empty |> add("A", 1) |> add("B", 2) |> add("C", 3));
let newMap =
  StringMap.(oldMap |> remove("A") |> add("D", 4) |> add("C", 5));

let addedItems =
  StringMap.symmetric_diff(
    oldMap, newMap, ~acc=[], ~veq=(==), ~f=(diffRes, acc) =>
    switch (diffRes) {
    | (key, Left(_a)) =>
      Js.log("Item with key " ++ key ++ " is only in the old map.");
      acc;
    | (key, Right(a)) =>
      Js.log("Item with key " ++ key ++ " is only in the new map.");
      [a, ...acc];
    | (key, Unequal(a, b)) =>
      Js.log(
        "Item with key "
        ++ key
        ++ " is in both, but its value was changed from "
        ++ string_of_int(a)
        ++ " to "
        ++ string_of_int(b)
        ++ ".",
      );
      acc;
    }
  );
// Item with key A is only in the old map.
// Item with key C is in both, but its value was changed from 3 to 5.
// Item with key D is only in the new map.

Js.log(Array.of_list(addedItems));
// [ 4 ]

Development

Build

npm run build

Build + Watch

npm run start

Run example

E.g.:

node lib/js/examples/MapDiffExample.js

Dependencies (2)

  1. reason >= "3.4.0"
  2. dune >= "1.6.0"

Dev Dependencies

None

Optional Dependencies

None

Used by

None

Conflicts

None

Build status

Results of building stdlib-diff with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.1.3 (this version) good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure