pipes-ordered-zip 1.0.1 → 1.1.0
raw patch · 6 files changed
+80/−34 lines, 6 filesdep +hspecdep +pipes-safedep ~pipesPVP ok
version bump matches the API change (PVP)
Dependencies added: hspec, pipes-safe
Dependency ranges changed: pipes
API changes (from Hackage documentation)
+ Pipes.OrderedZip: WrongInputOrderException :: String -> WrongInputOrderException
+ Pipes.OrderedZip: data WrongInputOrderException
+ Pipes.OrderedZip: instance GHC.Classes.Eq Pipes.OrderedZip.WrongInputOrderException
+ Pipes.OrderedZip: instance GHC.Exception.Type.Exception Pipes.OrderedZip.WrongInputOrderException
+ Pipes.OrderedZip: instance GHC.Show.Show Pipes.OrderedZip.WrongInputOrderException
+ Pipes.OrderedZip: orderCheckPipe :: (MonadIO m, MonadSafe m, Show a) => (a -> a -> Ordering) -> Pipe a a m r
Files
- Changelog.md +2/−0
- pipes-ordered-zip.cabal +11/−6
- src-tests/testMain.hs +0/−26
- src/Pipes/OrderedZip.hs +25/−2
- test/Pipes/OrderedZipSpec.hs +41/−0
- test/Spec.hs +1/−0
Changelog.md view
@@ -2,3 +2,5 @@ Initial commit with example, tests and haddock * Version 1.0.0.1: Added a note that input sequences have to be sorted.++* V 1.1.0: Added new function to check ordering of incoming pipes
pipes-ordered-zip.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: pipes-ordered-zip-version: 1.0.1+version: 1.1.0 license: BSD3 license-file: LICENSE copyright: 2019 Stephan Schiffels@@ -23,15 +23,20 @@ default-language: Haskell2010 build-depends: base >=4.7 && <5,- pipes >=4.3.9+ pipes >=4.3.11,+ pipes-safe >=2.3.1 test-suite pipes-ordered-zip-tests type: exitcode-stdio-1.0- main-is: testMain.hs- hs-source-dirs: src-tests+ main-is: Spec.hs+ hs-source-dirs: test+ other-modules:+ Pipes.OrderedZipSpec default-language: Haskell2010 build-depends: base >=4.12.0.0, pipes-ordered-zip -any,- pipes >=4.3.9,- foldl >=1.4.5+ pipes >=4.3.11,+ foldl >=1.4.5,+ hspec >=2.7.1,+ pipes-safe >=2.3.1
− src-tests/testMain.hs
@@ -1,26 +0,0 @@-import System.Exit (exitFailure)--import Control.Foldl (list, purely)-import Control.Monad (when)-import Pipes (each, Producer)-import Pipes.Prelude (fold')--import Pipes.OrderedZip (orderedZip)--main = do- let a = each aVec- b = each bVec- let mergedProd = orderedZip compare a b- (mergedList, _) <- purely fold' list mergedProd- when (mergedList /= result) exitFailure- where- aVec = [1,3,4,6,8]- bVec = [2,3,4,5,8]- result = [(Just 1, Nothing),- (Nothing, Just 2),- (Just 3, Just 3),- (Just 4, Just 4),- (Nothing, Just 5),- (Just 6, Nothing),- (Just 8, Just 8)]-
src/Pipes/OrderedZip.hs view
@@ -1,7 +1,11 @@ -- |A function to tie together two sorted Haskell Iterators-module Pipes.OrderedZip (orderedZip) where+module Pipes.OrderedZip (orderedZip, orderCheckPipe, WrongInputOrderException(..)) where -import Pipes (Producer, next, yield, lift)+import Control.Exception (Exception)+import Control.Monad.IO.Class (MonadIO, liftIO)+import Data.IORef (newIORef, readIORef, writeIORef)+import Pipes (Producer, next, yield, lift, Pipe, for, cat)+import Pipes.Safe (MonadSafe, throwM) -- |orderedZip takes a comparison function and two producers and merges them -- together, creating a new Producer that yields pairs of Maybes of the two @@ -41,3 +45,22 @@ yield (Nothing, Just p2a) p2Front' <- lift $ next p2Rest go ord' p1Front p1' p2Front' p2Rest++-- an exception type to represent invalid input order+data WrongInputOrderException = WrongInputOrderException String deriving (Show, Eq)+instance Exception WrongInputOrderException++-- a pipe to check wether the stream is ordered according to a custom ordering function+orderCheckPipe :: (MonadIO m, MonadSafe m, Show a) => (a -> a -> Ordering) -- ^the custom ordering function+ -> Pipe a a m r -- ^the resulting pipe+orderCheckPipe cmpFunc = do+ lastValRef <- liftIO $ newIORef (Nothing)+ for cat $ \entry -> do+ lastVal <- liftIO $ readIORef lastValRef+ case lastVal of+ Nothing -> return ()+ Just p -> case cmpFunc entry p of+ LT -> throwM $ WrongInputOrderException ("ordering violated: " ++ show p ++ " should come after " ++ show entry)+ _ -> return ()+ liftIO $ writeIORef lastValRef (Just entry)+ yield entry
+ test/Pipes/OrderedZipSpec.hs view
@@ -0,0 +1,41 @@+module Pipes.OrderedZipSpec (spec) where++import Control.Foldl (list, purely)+import Test.Hspec+import Pipes (each, (>->))+import Pipes.Prelude (fold')+import Pipes.Safe (runSafeT)++import Pipes.OrderedZip (orderedZip, orderCheckPipe, WrongInputOrderException(..))++spec :: Spec+spec = do+ testOrderedZip+ testOrderCheckPipe++testOrderedZip :: Spec+testOrderedZip = describe "orderedZip" $ do+ let a = each ([1,3,4,6,8] :: [Int])+ b = each ([2,3,4,5,8] :: [Int])+ mergedProd = orderedZip compare a b+ it "should give correctly merged output" $+ (fst <$> purely fold' list mergedProd) `shouldReturn` result+ where+ result = [(Just 1, Nothing),+ (Nothing, Just 2),+ (Just 3, Just 3),+ (Just 4, Just 4),+ (Nothing, Just 5),+ (Just 6, Nothing),+ (Just 8, Just 8)]++testOrderCheckPipe :: Spec+testOrderCheckPipe = describe "orderCheckPipe" $ do+ let a = each ([1,4,3,6,8] :: [Int])+ p = a >-> orderCheckPipe compare+ it "should throw if invalid input order is given" $+ runSafeT (purely fold' list p) `shouldThrow` (==WrongInputOrderException "ordering violated: 4 should come after 3")+ it "should do nothing if input is ordered" $ do+ let a' = each ([1,3,4,6,8] :: [Int])+ p' = a' >-> orderCheckPipe compare+ runSafeT (fst <$> purely fold' list p') `shouldReturn` [1, 3, 4, 6, 8]
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}