2015-11-18 05:21:51 +03:00
|
|
|
module Patch where
|
2015-11-18 05:23:53 +03:00
|
|
|
|
2016-01-14 19:18:57 +03:00
|
|
|
-- | An operation to replace, insert, or delete an item.
|
2015-11-18 05:44:55 +03:00
|
|
|
data Patch a =
|
|
|
|
Replace a a
|
|
|
|
| Insert a
|
|
|
|
| Delete a
|
2015-11-18 05:51:36 +03:00
|
|
|
deriving (Functor, Show, Eq)
|
2015-11-20 02:26:40 +03:00
|
|
|
|
2016-01-14 19:18:57 +03:00
|
|
|
-- | Return the item from the after side of the patch.
|
2015-11-20 02:26:40 +03:00
|
|
|
after :: Patch a -> Maybe a
|
|
|
|
after (Replace _ a) = Just a
|
|
|
|
after (Insert a) = Just a
|
|
|
|
after _ = Nothing
|
2015-11-20 04:25:28 +03:00
|
|
|
|
2016-01-14 19:18:57 +03:00
|
|
|
-- | Return the item from the before side of the patch.
|
2015-11-20 04:25:28 +03:00
|
|
|
before :: Patch a -> Maybe a
|
|
|
|
before (Replace a _) = Just a
|
|
|
|
before (Delete a) = Just a
|
|
|
|
before _ = Nothing
|
2015-12-01 03:08:28 +03:00
|
|
|
|
2016-01-14 19:18:57 +03:00
|
|
|
-- | Calculate the cost of the patch given a function to compute the cost of a item.
|
2015-12-01 03:08:28 +03:00
|
|
|
patchSum :: (a -> Integer) -> Patch a -> Integer
|
|
|
|
patchSum termCost patch = (maybe 0 termCost $ before patch) + (maybe 0 termCost $ after patch)
|