Diff-1.0.2: Diff algorithm in pure Haskell

Copyright | (c) Sterling Clover 2008-2011 Kevin Charter 2011
License | BSD 3 Clause
Maintainer | s.clover@gmail.com
Stability | experimental
Portability | portable
Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.Algorithm.Diff

Contents

- Comparing lists for differences
- Finding chunks of differences

Description

This is an implementation of the diff algorithm as described in An \( O(ND) \) Difference Algorithm and Its Variations (1986) http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927. For inputs of size \( O(N) \) with the number of differences \( D \) it has \( O(ND) \) time and \( O(D^2) \) space complexity.

Synopsis

- type Diff a = PolyDiff a a
- data PolyDiff a b
  - = First a
  - | Second b
  - | Both a b
- getDiff :: Eq a => [a] -> [a] -> [Diff a]
- getDiffBy :: (a -> b -> Bool) -> [a] -> [b] -> [PolyDiff a b]
- getGroupedDiff :: Eq a => [a] -> [a] -> [Diff [a]]
- getGroupedDiffBy :: (a -> b -> Bool) -> [a] -> [b] -> [PolyDiff [a] [b]]

# Documentation

type Diff a = PolyDiff a a Source #

This is `PolyDiff` specialized so both sides are the same type.

data PolyDiff a b Source #

A value is either from the `First` list, the `Second` or from `Both`. `Both` contains both the left and right values, in case you are using a form of equality that doesn't check all data (for example, if you are using a newtype to only perform equality on side of a tuple).

Constructors

First a |
Second b |
Both a b |

#### Instances

Instances details

Bifunctor PolyDiff Source # |
Instance details Defined in Data.Algorithm.Diff Methods bimap :: (a -> b) -> (c -> d) -> PolyDiff a c -> PolyDiff b d Source # first :: (a -> b) -> PolyDiff a c -> PolyDiff b c Source # second :: (b -> c) -> PolyDiff a b -> PolyDiff a c Source #
Functor (PolyDiff a) Source # |
Instance details Defined in Data.Algorithm.Diff Methods fmap :: (a0 -> b) -> PolyDiff a a0 -> PolyDiff a b Source # (<$) :: a0 -> PolyDiff a b -> PolyDiff a a0 Source #
(Show a, Show b) => Show (PolyDiff a b) Source # |
Instance details Defined in Data.Algorithm.Diff Methods showsPrec :: Int -> PolyDiff a b -> ShowS Source # show :: PolyDiff a b -> String Source # showList :: [PolyDiff a b] -> ShowS Source #
(Eq a, Eq b) => Eq (PolyDiff a b) Source # |
Instance details Defined in Data.Algorithm.Diff Methods (==) :: PolyDiff a b -> PolyDiff a b -> Bool Source # (/=) :: PolyDiff a b -> PolyDiff a b -> Bool Source #

# Comparing lists for differences

getDiff :: Eq a => [a] -> [a] -> [Diff a] Source #

Takes two lists and returns a list of differences between them. This is `getDiffBy` with `==` used as predicate.

    > getDiff ["a","b","c","d","e"] ["a","c","d","f"]
    [Both "a" "a",First "b",Both "c" "c",Both "d" "d",First "e",Second "f"]
    > getDiff "abcde" "acdf"
    [Both 'a' 'a',First 'b',Both 'c' 'c',Both 'd' 'd',First 'e',Second 'f']

getDiffBy :: (a -> b -> Bool) -> [a] -> [b] -> [PolyDiff a b] Source #

A form of `getDiff` with no `Eq` constraint. Instead, an equality predicate is taken as the first argument.

# Finding chunks of differences

getGroupedDiff :: Eq a => [a] -> [a] -> [Diff [a]] Source #

Takes two lists and returns a list of differences between them, grouped into chunks. This is `getGroupedDiffBy` with `==` used as predicate.

    > getGroupedDiff "abcde" "acdf"
    [Both "a" "a",First "b",Both "cd" "cd",First "e",Second "f"]

getGroupedDiffBy :: (a -> b -> Bool) -> [a] -> [b] -> [PolyDiff [a] [b]] Source #