@@ -15,7 +15,8 @@ import Data.List.NonEmpty as NEL
1515import Data.Maybe (Maybe (..))
1616import Data.NonEmpty ((:|))
1717import Data.StrMap as M
18- import Data.Tuple (Tuple (..), fst )
18+ import Data.Tuple (Tuple (..), fst , uncurry )
19+ import Data.Traversable (traverse , sequence )
1920
2021import Partial.Unsafe (unsafePartial )
2122
@@ -28,6 +29,11 @@ newtype TestStrMap v = TestStrMap (M.StrMap v)
2829instance arbTestStrMap :: (Arbitrary v ) => Arbitrary (TestStrMap v ) where
2930 arbitrary = TestStrMap <<< (M .fromFoldable :: L.List (Tuple String v ) -> M.StrMap v ) <$> arbitrary
3031
32+ newtype SmallArray v = SmallArray (Array v )
33+
34+ instance arbSmallArray :: (Arbitrary v ) => Arbitrary (SmallArray v ) where
35+ arbitrary = SmallArray <$> Gen .resize 3 arbitrary
36+
3137data Instruction k v = Insert k v | Delete k
3238
3339instance showInstruction :: (Show k , Show v ) => Show (Instruction k v ) where
@@ -54,6 +60,14 @@ runInstructions instrs t0 = foldl step t0 instrs
5460number :: Int -> Int
5561number n = n
5662
63+ oldTraverse :: forall a b m . Applicative m => (a -> m b ) -> M.StrMap a -> m (M.StrMap b )
64+ oldTraverse f ms = A .foldr (\x acc -> M .union <$> x <*> acc) (pure M .empty) ((map (uncurry M .singleton)) <$> (traverse f <$> (M .toUnfoldable ms :: Array (Tuple String a ))))
65+ oldSequence :: forall a m . Applicative m => M.StrMap (m a ) -> m (M.StrMap a )
66+ oldSequence = oldTraverse id
67+
68+ toAscArray :: forall a . M.StrMap a -> Array (Tuple String a )
69+ toAscArray = M .toAscUnfoldable
70+
5771strMapTests :: forall eff . Eff (console :: CONSOLE , random :: RANDOM , exception :: EXCEPTION | eff ) Unit
5872strMapTests = do
5973 log " Test inserting into empty tree"
@@ -167,6 +181,11 @@ strMapTests = do
167181 resultViaLists = m # M .toUnfoldable # map (\(Tuple k v) → Tuple k (f k v)) # (M .fromFoldable :: forall a . L.List (Tuple String a ) -> M.StrMap a )
168182 in resultViaMapWithKey === resultViaLists
169183
184+ log " sequence gives the same results as an old version (up to ordering)"
185+ quickCheck \(TestStrMap mOfSmallArrays :: TestStrMap (SmallArray Int )) ->
186+ let m = (\(SmallArray a) -> a) <$> mOfSmallArrays
187+ in A .sort (toAscArray <$> oldSequence m) === A .sort (toAscArray <$> sequence m)
188+
170189 log " Bug #63: accidental observable mutation in foldMap"
171190 quickCheck \(TestStrMap m) ->
172191 let lhs = go m
0 commit comments