mono-traversable 0.3.0.3 → 0.3.1
raw patch · 3 files changed
+37/−1 lines, 3 filesdep +foldl
Dependencies added: foldl
Files
- mono-traversable.cabal +2/−1
- src/Data/MonoTraversable.hs +24/−0
- test/Spec.hs +11/−0
mono-traversable.cabal view
@@ -1,5 +1,5 @@ name: mono-traversable-version: 0.3.0.3+version: 0.3.1 synopsis: Type classes for mapping, folding, and traversing monomorphic containers description: Monomorphic variants of the Functor, Foldable, and Traversable typeclasses. Contains even more experimental code for abstracting containers and sequences. homepage: https://github.com/snoyberg/mono-traversable@@ -52,6 +52,7 @@ , semigroups , containers , unordered-containers+ , foldl source-repository head type: git
src/Data/MonoTraversable.hs view
@@ -824,3 +824,27 @@ oforM :: (MonoTraversable mono, Monad f) => mono -> (Element mono -> f (Element mono)) -> f mono oforM = flip omapM {-# INLINE oforM #-}++-- | A strict left fold, together with an unwrap function.+--+-- This is convenient when the accumulator value is not the same as the final+-- expected type. It is provided mainly for integration with the @foldl@+-- package, to be used in conjunction with @purely@.+--+-- Since 0.3.1+ofoldlUnwrap :: MonoFoldable mono+ => (x -> Element mono -> x) -> x -> (x -> b) -> mono -> b+ofoldlUnwrap f x unwrap mono = unwrap (ofoldl' f x mono)++-- | A monadic strict left fold, together with an unwrap function.+--+-- Similar to @foldlUnwrap@, but allows monadic actions. To be used with+-- @impurely@ from @foldl@.+--+-- Since 0.3.1+ofoldMUnwrap :: (Monad m, MonoFoldable mono)+ => (x -> Element mono -> m x) -> m x -> (x -> m b) -> mono -> m b+ofoldMUnwrap f mx unwrap mono = do+ x <- mx+ x' <- ofoldlM f x mono+ unwrap x'
test/Spec.hs view
@@ -29,6 +29,7 @@ import Data.Containers import qualified Data.IntSet as IntSet import Control.Arrow (first, second)+import qualified Control.Foldl as Foldl main :: IO () main = hspec $ do@@ -274,3 +275,13 @@ test "Data.Map" Map.empty Map.lookup Map.insert Map.delete test "Data.IntMap" IntMap.empty IntMap.lookup IntMap.insert IntMap.delete test "Data.HashMap" HashMap.empty HashMap.lookup HashMap.insert HashMap.delete++ describe "foldl integration" $ do+ prop "vector" $ \xs -> do+ x1 <- Foldl.foldM Foldl.vector (xs :: [Int])+ x2 <- Foldl.impurely ofoldMUnwrap Foldl.vector xs+ x2 `shouldBe` (x1 :: V.Vector Int)+ prop "length" $ \xs -> do+ let x1 = Foldl.fold Foldl.length (xs :: [Int])+ x2 = Foldl.purely ofoldlUnwrap Foldl.length xs+ x2 `shouldBe` x1