packages feed

merge (empty) → 0.1.0.0

raw patch · 6 files changed

+150/−0 lines, 6 filesdep +basedep +mergedep +profunctors

Dependencies added: base, merge, profunctors

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for merge++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,9 @@+Copyright 2020 Samuel Schlesinger++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.++
+ README.md view
@@ -0,0 +1,17 @@+# Merge++```haskell+data User = User+  { name :: Maybe Text+  , pubKey :: PublicKey+  }++mergeUsers :: Merge User User+mergeUsers =+  User+    <$> optional name+    <*> required pubKey++f :: User -> User -> Maybe User+f x y = runMerge mergeUsers x y+```
+ merge.cabal view
@@ -0,0 +1,31 @@+cabal-version:      2.4+name:               merge+version:            0.1.0.0+synopsis:           A functor for consistent merging of information+description:        A functor for consistent merging of information.+author:             Samuel Schlesinger+maintainer:         sgschlesinger@gmail.com+extra-source-files: CHANGELOG.md+license:            MIT+license-file:       LICENSE+copyright:          2020 Samuel Schlesinger+category:           Data+extra-source-files: CHANGELOG.md, README.md+build-type:         Simple++source-repository head+  type: git+  location: https://github.com/samuelschlesinger/merge++library+    exposed-modules:  Data.Merge+    build-depends:    base >=4.6 && <5, profunctors >=5.6 && <6+    hs-source-dirs:   src+    default-language: Haskell2010++test-suite test+  type: exitcode-stdio-1.0+  main-is: Test.hs+  hs-source-dirs: test+  build-depends: base >= 4.6 && <5, merge+  default-language: Haskell2010
+ src/Data/Merge.hs view
@@ -0,0 +1,66 @@+{- |+Name: Data.Merge+Description: To describe merging of data types.+License: MIT+Copyright: Samuel Schlesinger 2021 (c)+-}+{-# LANGUAGE BlockArguments #-}+module Data.Merge where++import Control.Monad (join)+import Control.Applicative (Alternative (..))+import Data.Profunctor (Profunctor (..))++-- | Describes the merging of two values of the same type+-- into some other type. Represented as a 'Maybe' valued+-- function, one can also think of this as a predicate+-- showing which pairs of values can be merged in this way.+--+-- > data Example = Whatever { a :: Int, b :: Maybe Bool }+-- > mergeExamples :: Merge Example Example+-- > mergeExamples = Example <$> required a <*> optional b+newtype Merge x a = Merge { runMerge :: x -> x -> Maybe a }++-- | The most general combinator for constructing 'Merge's.+merge :: (x -> x -> Maybe a) -> Merge x a+merge = Merge++instance Profunctor Merge where+  dimap l r (Merge f) = Merge \x x' -> r <$> f (l x) (l x')++instance Functor (Merge x) where+  fmap = rmap++instance Applicative (Merge x) where+  pure x = Merge (\_ _ -> Just x)+  fa <*> a = Merge \x x' -> runMerge fa x x' <*> runMerge a x x'++instance Alternative (Merge x) where+  empty = Merge \_ _ -> Nothing+  Merge f <|> Merge g = Merge \x x' -> f x x' <|> g x x'++instance Monad (Merge x) where+  a >>= f = Merge \x x' -> join $ fmap (\b -> runMerge b x x') $ fmap f $ runMerge a x x'++instance Semigroup a => Semigroup (Merge x a) where+  a <> b = Merge \x x' -> runMerge a x x' <> runMerge b x x'++instance Semigroup a => Monoid (Merge x a) where+  mempty = Merge \_ _ -> mempty  ++-- | Meant to be used to merge optional fields in a record.+optional :: Eq a => (x -> Maybe a) -> Merge x (Maybe a)+optional f = Merge (\x x' -> go (f x) (f x'))  where+  go (Just x) (Just x')+    | x == x' = Just (Just x)+    | otherwise = Nothing+  go Nothing (Just x) = Just (Just x)+  go (Just x) Nothing = Just (Just x)+  go Nothing Nothing = Just Nothing++-- | Meant to be used to merge required fields in a record.+required :: Eq a => (x -> a) -> Merge x a+required f = Merge (\x x' -> go (f x) (f x'))  where+  go x x'+    | x == x' = Just x+    | otherwise = Nothing
+ test/Test.hs view
@@ -0,0 +1,22 @@+module Main where++import Control.Monad (mapM_)+import System.Exit (exitFailure, exitSuccess)+import Data.Merge++requires :: String -> [Bool] -> IO ()+requires msg = mapM_ (uncurry go) . zip [1..] where+  go n x+    | x = pure ()+    | otherwise = putStrLn (msg <> ": " <> show n) >> exitFailure++main :: IO ()+main = do+  let merge = (,) <$> optional fst <*> required snd+  requires "merge"+    [ runMerge merge (Just 10, 1) (Nothing, 1) == Just (Just 10, 1) +    , runMerge merge (Nothing, 1) (Nothing, 1) == Just (Nothing, 1)+    , runMerge merge (Nothing, 1) (Nothing, 2) == Nothing+    , runMerge merge (Just 10, 1) (Just 11, 1) == Nothing+    , runMerge merge (Just 10, 1) (Just 11, 2) == Nothing+    ]