ocharles / diff3

3-way diff algorithm for Haskell
Other
6 stars 1 forks source link

interop with Diff #3

Open mstksg opened 6 years ago

mstksg commented 6 years ago

Hi! Thanks for the great library.

Was wondering if there was any meaningful way to "merge" two [Diff a]'s into a [Hunk a], such that:

xs, ys, zs :: [A]

xsys, xszs :: [Diff A]
xsys = getDiff xs ys
xszs = getDiff xs zs

xsyszs :: [Hunk a]
xsyszs = diff3 ys xs zs
-- or
xsyszs = mergeDiffs xsys xszs

Where if you diff two lists from the same original lists and merge the resulting diffs, you get what you would if you had originally diff3'd all three together at the same time.

I see that toHunk in the source appears to have the right type signature -- I'm not exactly sure what it does, but does it give the behavior I am thinking of?

ocharles commented 6 years ago

I don't think toHunk is quite what you want - note that it's called here only in the degenerate cases where one of the lists is empty. Can I ask what your use case is?

mstksg commented 6 years ago

Actually I just realized that what I'm thinking about is exactly step from the implementation of diff3 :)

My particular use case is for working with "patches" as standalone data types, where a [Diff A] is a patch that can be applied to an [A]. (if it was generated using getDiff). The point is to be able to manipulate patches on their own and then re-apply when ready. Merging two patches is such an operation, and a [Diff A] -> [Diff A] -> [Diff A] would do the trick.

It looks like that is essentially what step is doing though, so I might be looking at that for a reference. Thanks for the pointer!

What do you feel about a PR possibly exporting step as a standalone function with its own name, with some descriptive documentation talking about when it is applicable?