Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Add Dictionary difference functions #19

Open
Hexcede opened this issue Jan 15, 2024 · 1 comment
Open

Add Dictionary difference functions #19

Hexcede opened this issue Jan 15, 2024 · 1 comment

Comments

@Hexcede
Copy link

Hexcede commented Jan 15, 2024

I have written some computationally & memory efficient implementations for these already which produce the exact behaviours I demonstrated in the example code already, so I'd be happy to write up a PR if that's desirable.

  • Sift.Dictionary.difference(dictA, dictB) - Would produce a new dictionary containing values which are different in B than in A. This only considers top-level equality. This is also the inverse of Sift.Dictionary.merge.
  • Sift.Dictionary.differenceDeep(dictA, dictB) - Would produce a new dictionary containing values which are different in B than in A. Sub-dictionaries would only contain differences as well. This is also the inverse of Sift.Dictionary.mergeDeep.
local kinds = {
	Apple = true;
	Pear = true;
	
	Others = {
		Carrot = true;
		Potato = true;
	};
}

local dictA = {
	Apple = 9;
	Pear = 7;

	Others = {
		Carrot = 11;
		Potato = 15;
	};

	Kinds = kinds;
}

local dictB = {
	Apple = 10;
	Pear = 7;

	Others = {
		Carrot = 12;
	};

	Kinds = kinds;
}

-- Shallow difference, only top level equality
Sift.Dictionary.difference(dictA, dictB) -- { Apple = 10, Others = { Carrot = 12 } }

-- Deep difference, sub-dictionaries will also be deeply diffed
Sift.Dictionary.differenceDeep(dictA, dictB) -- { Apple = 10, Others = { Carrot = 12, Potato = Sift.None }, Kinds = { } }

-- Another valid implementation might omit Kinds since it has no changes. This behaviour might not be expected in some cases though, so it might be worth considering which of the two implementations makes the most sense.
-- It might also be good to consider how table equality should be treated. In the example I assume just == behaviour, but `Dictionary.equals` or `Dictionary.equalsDeep` could also be used for diffing sub-dicts.
@cxmeel cxmeel mentioned this issue Mar 12, 2024
7 tasks
cxmeel added a commit that referenced this issue Mar 15, 2024
@Hexcede Hexcede closed this as completed Apr 18, 2024
@Hexcede Hexcede reopened this Apr 18, 2024
@Hexcede
Copy link
Author

Hexcede commented Apr 18, 2024

Oops, I thought the version containing the fix had been released for some reason, my bad.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant