pipes-ordered-zip 1.1.0 → 1.2.0
raw patch · 4 files changed
+117/−41 lines, 4 filesdep ~basedep ~foldldep ~hspecPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, foldl, hspec, pipes, pipes-safe
API changes (from Hackage documentation)
+ Pipes.OrderedZip: orderedZipAll :: Monad m => (a -> a -> Ordering) -> [Producer a m r] -> Producer [Maybe a] m [r]
Files
- README.md +27/−2
- pipes-ordered-zip.cabal +26/−27
- src/Pipes/OrderedZip.hs +43/−11
- test/Pipes/OrderedZipSpec.hs +21/−1
README.md view
@@ -9,8 +9,8 @@ import Pipes.OrderedZip (orderedZip) main = do- let a = each [1,3,4,6,8] -- has to be ordered- b = each [2,3,4,5,8] -- has to be ordered+ let a = each [1, 3,4, 6,8] -- assumed to be ordered+ b = each [ 2,3,4,5, 8] -- assumed to be ordered let mergedProd = orderedZip compare a b _ <- runEffect $ mergedProd >-> P.print return ()@@ -24,3 +24,28 @@ (Nothing,Just 5) (Just 6,Nothing) (Just 8,Just 8)++and++ import Pipes (runEffect, (>->), each)+ import qualified Pipes.Prelude as P+ import Pipes.OrderedZip (orderedZipAll)++ main = do+ let a = each ([1, 3,4, 6, 8] :: [Int])+ b = each ([ 2,3,4,5, 8] :: [Int])+ c = each ([ 2,3, 5, 7,8] :: [Int])+ mergedProd = orderedZipAll compare [a, b, c]+ _ <- runEffect $ mergedProd >-> P.print+ return ()++prints ++ [Just 1,Nothing,Nothing]+ [Nothing,Just 2,Just 2]+ [Just 3,Just 3,Just 3]+ [Just 4,Just 4,Nothing]+ [Nothing,Just 5,Just 5]+ [Just 6,Nothing,Nothing]+ [Nothing,Nothing,Just 7]+ [Just 8,Just 8,Just 8]
pipes-ordered-zip.cabal view
@@ -1,42 +1,41 @@-cabal-version: >=1.10-name: pipes-ordered-zip-version: 1.1.0-license: BSD3-license-file: LICENSE-copyright: 2019 Stephan Schiffels-maintainer: stephan_schiffels@mac.com-author: Stephan Schiffels-homepage: https://github.com/githubuser/pipes-ordered-zip#readme-synopsis: merge two ordered Producers into a new Producer+cabal-version: >=1.10+name: pipes-ordered-zip+version: 1.2.0+license: BSD3+license-file: LICENSE+copyright: 2019 Stephan Schiffels+maintainer: stephan_schiffels@mac.com+author: Stephan Schiffels+homepage: https://github.com/githubuser/pipes-ordered-zip#readme+synopsis: merge two ordered Producers into a new Producer description: provides a simple function to merge two Pipes-Producers into a new Producer that yields pairs of values of the original producers, but using Maybes to indicate values that are only present in the first, second or both producers. Useful to perform Left-Joins, Right-Joins and Inner-Joins on the fly using Haskell Pipes.-category: Bioinformatics-build-type: Simple++category: Bioinformatics+build-type: Simple extra-source-files: README.md Changelog.md library- exposed-modules:- Pipes.OrderedZip- hs-source-dirs: src+ exposed-modules: Pipes.OrderedZip+ hs-source-dirs: src default-language: Haskell2010 build-depends: base >=4.7 && <5,- pipes >=4.3.11,- pipes-safe >=2.3.1+ pipes >=4.3.15,+ pipes-safe >=2.3.3 test-suite pipes-ordered-zip-tests- type: exitcode-stdio-1.0- main-is: Spec.hs- hs-source-dirs: test- other-modules:- Pipes.OrderedZipSpec+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs: test+ other-modules: Pipes.OrderedZipSpec default-language: Haskell2010 build-depends:- base >=4.12.0.0,+ base >=4.14.1.0, pipes-ordered-zip -any,- pipes >=4.3.11,- foldl >=1.4.5,- hspec >=2.7.1,- pipes-safe >=2.3.1+ pipes >=4.3.15,+ foldl >=1.4.10,+ hspec >=2.7.8,+ pipes-safe >=2.3.3
src/Pipes/OrderedZip.hs view
@@ -1,20 +1,23 @@ -- |A function to tie together two sorted Haskell Iterators-module Pipes.OrderedZip (orderedZip, orderCheckPipe, WrongInputOrderException(..)) where+module Pipes.OrderedZip (orderedZip, orderedZipAll, orderCheckPipe, WrongInputOrderException(..)) where -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)+import Control.Exception (Exception)+import Control.Monad.IO.Class (MonadIO, liftIO)+import Data.IORef (newIORef, readIORef, writeIORef)+import Data.Maybe (catMaybes)+import Pipes (Pipe, Producer, cat, for, lift, next,+ yield, (>->))+import qualified Pipes.Prelude as P+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 +-- |orderedZip takes a comparison function and two producers and merges them+-- together, creating a new Producer that yields pairs of Maybes of the two -- datatables provided by the two original producers. -- The output pairs reflect the Union of the two input producers, with Nothings indicating -- missing data in one of the producers at that location. orderedZip :: (Monad m) => (a -> b -> Ordering) -- ^The function to compare types of a with b- -> Producer a m r1 -- ^The first producer (has to be ordered)- -> Producer b m r2 -- ^The second producer (has to be ordered)+ -> Producer a m r1 -- ^The first producer (assumed to be ordered)+ -> Producer b m r2 -- ^The second producer (assumed to be ordered) -> Producer (Maybe a, Maybe b) m (r1, r2) -- ^The merged producer orderedZip ord p1 p2 = do p1Front <- lift $ next p1@@ -46,8 +49,37 @@ p2Front' <- lift $ next p2Rest go ord' p1Front p1' p2Front' p2Rest +-- |orderedZipAll takes a comparison function and a list of producers and merges them+-- together, creating a new Producer that yields lists of Maybes of the+-- input data type provided by the original producers.+-- The output list reflects the Union of all input producers, with Nothings indicating+-- missing data in any of the producers at that instance.+orderedZipAll :: (Monad m) => (a -> a -> Ordering) -- ^The function to compare types of a with itself+ -> [Producer a m r] -- ^A list of producers (have to be ordered)+ -> Producer [Maybe a] m [r] -- ^The merged producer+orderedZipAll compFunc prodList = go compFunc (length prodList) prodList+ where+ go :: (Monad m) => (a -> a -> Ordering) -> Int -> [Producer a m r] -> Producer [Maybe a] m [r]+ go compFunc _ [] = error "orderedZipAll error: empty list" -- just to make the function complete+ go compFunc _ [prod] = fmap return (prod >-> P.map (return . Just))+ go compFunc n (prod1:prods) =+ (fmap (\(r, rs) -> (r:rs)) (orderedZip (compFunc2 compFunc) prod1 (go compFunc (n - 1) prods))) >-> P.map mergeMaybeTuples+ where+ mergeMaybeTuples :: (Maybe a, Maybe [Maybe a]) -> [Maybe a]+ mergeMaybeTuples (Nothing, Nothing) = error "orderedZipAll - should never happen" -- just to make this function complete+ mergeMaybeTuples (Nothing, Just mla) = (Nothing: mla)+ mergeMaybeTuples (Just a, Nothing) = (Just a : replicate (n - 1) Nothing)+ mergeMaybeTuples (Just a, Just mla) = (Just a : mla)+ compFunc2 :: (a -> a -> Ordering) -> a -> [Maybe a] -> Ordering+ compFunc2 compFunc x xs =+ let allJusts = catMaybes xs+ in case allJusts of+ [] -> error "orderedZipAll compFunc2 - should never happen" -- just to make this complete+ (xs1:_) -> compFunc x xs1+ -- an exception type to represent invalid input order-data WrongInputOrderException = WrongInputOrderException String deriving (Show, Eq)+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
test/Pipes/OrderedZipSpec.hs view
@@ -6,11 +6,12 @@ import Pipes.Prelude (fold') import Pipes.Safe (runSafeT) -import Pipes.OrderedZip (orderedZip, orderCheckPipe, WrongInputOrderException(..))+import Pipes.OrderedZip (orderedZip, orderedZipAll, orderCheckPipe, WrongInputOrderException(..)) spec :: Spec spec = do testOrderedZip+ testOrderedZipAll testOrderCheckPipe testOrderedZip :: Spec@@ -29,6 +30,24 @@ (Just 6, Nothing), (Just 8, Just 8)] +testOrderedZipAll :: Spec+testOrderedZipAll = describe "orderedZipAll" $ do+ let a = each ([1, 3,4, 6, 8] :: [Int])+ b = each ([ 2,3,4,5, 8] :: [Int])+ c = each ([ 2,3, 5, 7,8] :: [Int])+ mergedProd = orderedZipAll compare [a, b, c]+ it "should give correctly merged output" $+ (fst <$> purely fold' list mergedProd) `shouldReturn` result+ where+ result = [[Just 1, Nothing, Nothing],+ [Nothing, Just 2, Just 2],+ [Just 3, Just 3, Just 3],+ [Just 4, Just 4, Nothing],+ [Nothing, Just 5, Just 5],+ [Just 6, Nothing, Nothing],+ [Nothing, Nothing, Just 7],+ [Just 8, Just 8, Just 8]]+ testOrderCheckPipe :: Spec testOrderCheckPipe = describe "orderCheckPipe" $ do let a = each ([1,4,3,6,8] :: [Int])@@ -39,3 +58,4 @@ 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]+