@@ -7,12 +7,13 @@ import Control.Monad.Eff.Console (log, CONSOLE)
77import Control.Monad.Eff.Exception (EXCEPTION )
88import Control.Monad.Eff.Random (RANDOM )
99
10+ import Data.Array as A
1011import Data.Foldable (foldl )
1112import Data.Function (on )
12- import Data.List ( List (..), groupBy , sortBy , singleton , fromFoldable , zipWith )
13+ import Data.List as L
1314import Data.List.NonEmpty as NEL
14- import Data.NonEmpty ((:|))
1515import Data.Maybe (Maybe (..))
16+ import Data.NonEmpty ((:|))
1617import Data.StrMap as M
1718import Data.Tuple (Tuple (..), fst )
1819
@@ -25,7 +26,7 @@ import Test.QuickCheck.Gen as Gen
2526newtype TestStrMap v = TestStrMap (M.StrMap v )
2627
2728instance arbTestStrMap :: (Arbitrary v ) => Arbitrary (TestStrMap v ) where
28- arbitrary = TestStrMap <<< (M .fromFoldable :: List (Tuple String v ) -> M.StrMap v ) <$> arbitrary
29+ arbitrary = TestStrMap <<< (M .fromFoldable :: L. List (Tuple String v ) -> M.StrMap v ) <$> arbitrary
2930
3031data Instruction k v = Insert k v | Delete k
3132
@@ -36,15 +37,15 @@ instance showInstruction :: (Show k, Show v) => Show (Instruction k v) where
3637instance arbInstruction :: (Arbitrary v ) => Arbitrary (Instruction String v ) where
3738 arbitrary = do
3839 b <- arbitrary
39- k <- Gen .frequency $ Tuple 10.0 (pure " hasOwnProperty" ) :| Tuple 50.0 arbitrary ` Cons ` Nil
40+ k <- Gen .frequency $ Tuple 10.0 (pure " hasOwnProperty" ) :| pure ( Tuple 50.0 arbitrary)
4041 case b of
4142 true -> do
4243 v <- arbitrary
4344 pure (Insert k v)
4445 false -> do
4546 pure (Delete k)
4647
47- runInstructions :: forall v . List (Instruction String v ) -> M.StrMap v -> M.StrMap v
48+ runInstructions :: forall v . L. List (Instruction String v ) -> M.StrMap v -> M.StrMap v
4849runInstructions instrs t0 = foldl step t0 instrs
4950 where
5051 step tree (Insert k v) = M .insert k v tree
@@ -101,7 +102,7 @@ strMapTests = do
101102 in M .lookup k tree == Just v <?> (" instrs:\n " <> show instrs <> " \n k:\n " <> show k <> " \n v:\n " <> show v)
102103
103104 log " Singleton to list"
104- quickCheck $ \k v -> M .toList (M .singleton k v :: M.StrMap Int ) == singleton (Tuple k v)
105+ quickCheck $ \k v -> M .toUnfoldable (M .singleton k v :: M.StrMap Int ) == L . singleton (Tuple k v)
105106
106107 log " fromFoldable [] = empty"
107108 quickCheck (M .fromFoldable [] == (M .empty :: M.StrMap Unit )
@@ -125,26 +126,26 @@ strMapTests = do
125126 quickCheck (M .lookup " 1" nums == Just 2 <?> " invalid lookup - 1" )
126127 quickCheck (M .lookup " 2" nums == Nothing <?> " invalid lookup - 2" )
127128
128- log " toList . fromFoldable = id"
129- quickCheck $ \arr -> let f x = M .toList (M .fromFoldable x)
130- in f (f arr) == f (arr :: List (Tuple String Int )) <?> show arr
129+ log " toUnfoldable . fromFoldable = id"
130+ quickCheck $ \arr -> let f x = M .toUnfoldable (M .fromFoldable x)
131+ in f (f arr) == f (arr :: L. List (Tuple String Int )) <?> show arr
131132
132- log " fromFoldable . toList = id"
133+ log " fromFoldable . toUnfoldable = id"
133134 quickCheck $ \(TestStrMap m) ->
134- let f m1 = M .fromFoldable (M .toList m1) in
135- M .toList (f m) == M .toList ( m :: M.StrMap Int ) <?> show m
135+ let f m1 = M .fromFoldable (( M .toUnfoldable m1) :: L.List ( Tuple String Int ) ) in
136+ M .toUnfoldable (f m) == ( M .toUnfoldable m :: L.List ( Tuple String Int ) ) <?> show m
136137
137138 log " fromFoldableWith const = fromFoldable"
138139 quickCheck $ \arr -> M .fromFoldableWith const arr ==
139- M .fromFoldable (arr :: List (Tuple String Int )) <?> show arr
140+ M .fromFoldable (arr :: L. List (Tuple String Int )) <?> show arr
140141
141142 log " fromFoldableWith (<>) = fromFoldable . collapse with (<>) . group on fst"
142143 quickCheck $ \arr ->
143144 let combine (Tuple s a) (Tuple t b) = (Tuple s $ b <> a)
144- foldl1 g = unsafePartial \(Cons x xs) -> foldl g x xs
145+ foldl1 g = unsafePartial \(L. Cons x xs) -> foldl g x xs
145146 f = M .fromFoldable <<< map (foldl1 combine <<< NEL .toList) <<<
146- groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
147- M .fromFoldableWith (<>) arr == f (arr :: List (Tuple String String )) <?> show arr
147+ L . groupBy ((==) `on` fst) <<< L. sortBy (compare `on` fst) in
148+ M .fromFoldableWith (<>) arr == f (arr :: L. List (Tuple String String )) <?> show arr
148149
149150 log " Lookup from union"
150151 quickCheck $ \(TestStrMap m1) (TestStrMap m2) k ->
@@ -157,13 +158,13 @@ strMapTests = do
157158 (m1 `M.union` m2) == ((m1 `M.union` m2) `M.union` (m2 :: M.StrMap Int)) <?> (show (M.size (m1 `M.union` m2)) <> " != " <> show (M.size ((m1 `M.union` m2) `M.union` m2)))
158159
159160 log " fromFoldable = zip keys values"
160- quickCheck $ \(TestStrMap m) -> M .toList m == zipWith Tuple (fromFoldable $ M .keys m) (M .values m :: List Int )
161+ quickCheck $ \(TestStrMap m) -> M .toUnfoldable m == A . zipWith Tuple (M .keys m) (M .values m :: Array Int )
161162
162163 log " mapWithKey is correct"
163164 quickCheck $ \(TestStrMap m :: TestStrMap Int ) -> let
164165 f k v = k <> show v
165166 resultViaMapWithKey = m # M .mapWithKey f
166- resultViaLists = m # M .toList # map (\(Tuple k v) → Tuple k (f k v)) # M .fromFoldable
167+ 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 )
167168 in resultViaMapWithKey === resultViaLists
168169
169170 log " Bug #63: accidental observable mutation in foldMap"
0 commit comments