npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ocaml-diff

v0.1.3

Published

Symmetric Diffs for OCaml stdlib and ReasonML

Downloads

11

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