Janet 1.39.1-e9c6678 Documentation
(Other Versions: 1.38.0 1.37.1 1.36.0 1.35.0 1.34.0 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

data

Clojure contains a very useful core library (or "namespace" in Clojure parlance) called clojure.data (source). It contains one "exported" function: clojure.data/diff. This addition to spork, data.janet, should exactly replicate the behavior of clojure.data/diff using Janet tables, structs, arrays, and tuples in place of their Clojure equivalents.

Function

The diff function recursively compares the structure and contents of two data structures (struct, table, tuple, array) and returns an array with three elements:

@[things-only-in-a things-only-in-b things-in-both]

In the case of nested associative data structures (i.e., tables and structs), the comparison is recursive and the data structures are neatly partitioned into the same @[things-only-in-a things-only-in-b things-in-both] structure, but arbitrary levels deep in the two original associative data structures.

This function makes comparing two structs or tables for changes trivial. (An example use case: compare the decoded JSON returned from a REST API call made seconds ago against the version of that same decoded JSON from that same API that was returned from the same call made an hour ago and stored locally in a database for comparison an hour later.)

Example

So for example, diff'ing the two nested structs {:a 1 :b 2 :c {:d 3 :e 4}} and {:a 4 :b 2 :c {:d 3 :e 5 :f 6}} looks like this:

repl:1:> (import spork/data :as d)
repl:2:> (d/diff {:a 1 :b 2 :c {:d 3 :e 4}} {:a 4 :b 2 :c {:d 3 :e 5 :f 6}})
@[@{:a 1 :c @{:e 4}} @{:a 4 :c @{:e 5 :f 6}} @{:b 2 :c @{:d 3}}]

The return is @[@{:a 1 :c @{:e 4}} @{:a 4 :c @{:e 5 :f 6}} @{:b 2 :c @{:d 3}}] because:

Reference

data/diff


data/diff var (function)
(diff a b)

 
Compares a and b recursively. Returns an array of 
@[things-only-in-a things-only-in-b things-in-both].