@@ -1278,76 +1278,54 @@ instance Arbitrary WhenMatchedSpec where
1278
1278
1279
1279
----------------------------------------------------------------
1280
1280
1281
- -- fromAscListWith, fromAscListWithKey, fromDescListWith, fromDescListWithKey
1282
- -- all effectively perform a left fold over adjacent elements in the input list
1283
- -- using some function as long as the keys are equal.
1284
- --
1285
- -- The property tests for these functions compare the result against the
1286
- -- sequence we would get if we used NE.groupBy instead. We use Magma to check
1287
- -- the fold direction (left, not right) and the order of arguments to the fold
1288
- -- function (new then old).
1289
-
1290
1281
data Magma a
1291
1282
= Inj a
1292
1283
| Magma a :* Magma a
1293
1284
deriving (Eq , Show )
1294
1285
1295
- groupByK :: Eq k => [(k , a )] -> [(k , NonEmpty a )]
1296
- groupByK =
1297
- List. map (\ ys -> (fst (NE. head ys), NE. map snd ys)) .
1298
- NE. groupBy ((==) `on` fst )
1299
-
1300
1286
prop_list :: [Int ] -> Bool
1301
1287
prop_list xs = (sort (nub xs) == [x | (x,() ) <- toList (fromList [(x,() ) | x <- xs])])
1302
1288
1303
1289
prop_descList :: [Int ] -> Bool
1304
1290
prop_descList xs = (reverse (sort (nub xs)) == [x | (x,() ) <- toDescList (fromList [(x,() ) | x <- xs])])
1305
1291
1306
- -- For a list with keys in decreasing order (downSortedKxs), the sequence we get from fromDescList
1307
- -- should be the same as the reverse of the sequence we get if we group by the key and take the last
1308
- -- key-value pair in each group.
1309
1292
prop_fromDescList :: [(Int , A )] -> Property
1310
1293
prop_fromDescList kxs =
1311
1294
valid t .&&.
1312
- toList t === reverse nubLastDownSortedXs
1295
+ t === fromList kxs
1313
1296
where
1314
1297
downSortedKxs = List. sortBy (comparing (Down . fst )) kxs
1315
- nubLastDownSortedXs = [(k, NE. last xs) | (k,xs) <- groupByK downSortedKxs]
1316
1298
t = fromDescList downSortedKxs
1317
1299
1318
- -- For a list with keys in decreasing order (downSortedKxs), the sequence we get from
1319
- -- fromDescListWith f should be the same as the reverse of the sequence we get if we group by the
1320
- -- key and fold all the values for a key with f. fromDescListWith applies f as `f newv oldv`.
1321
1300
prop_fromDescListWith :: [(Int , A )] -> Property
1322
1301
prop_fromDescListWith kys =
1323
1302
valid t .&&.
1324
- toList t === reverse foldedDownSortedXs
1303
+ t === fromListWith (:*) downSortedKxs
1325
1304
where
1326
1305
kxs = [(k, Inj y) | (k,y) <- kys]
1327
1306
downSortedKxs = List. sortBy (comparing (Down . fst )) kxs
1328
- foldedDownSortedXs = [(k, Foldable. foldl1 (flip (:*) ) xs) | (k,xs) <- groupByK downSortedKxs]
1329
1307
t = fromDescListWith (:*) downSortedKxs
1330
1308
1331
- -- For a list with keys in decreasing order (downSortedKxs), the sequence we get from
1332
- -- fromDescListWithKey f should be the same as the reverse of the sequence we get if we group by the
1333
- -- key and fold all the values for a key with f. fromDescListWith applies f as `f key newv oldv`.
1334
1309
prop_fromDescListWithKey :: [(Int , A )] -> Property
1335
1310
prop_fromDescListWithKey kys =
1336
1311
valid t .&&.
1337
- toList t === reverse foldedDownSortedXs
1312
+ t === fromListWithKey f downSortedKxs
1338
1313
where
1339
1314
kxs = [(k, Inj (Left y)) | (k,y) <- kys]
1340
1315
downSortedKxs = List. sortBy (comparing (Down . fst )) kxs
1341
- foldedDownSortedXs = [ (k, Foldable. foldl1 ( \ acc ( Inj (Left x)) -> Inj (Right (k,x)) :* acc) xs)
1342
- | (k,xs) <- groupByK downSortedKxs ]
1343
- t = fromDescListWithKey ( \ k ( Inj ( Left x)) acc -> Inj ( Right (k,x)) :* acc) downSortedKxs
1316
+ f k ( Inj (Left x)) acc = Inj (Right (k,x)) :* acc
1317
+ f _ _ _ = error " prop_fromDescListWithKey "
1318
+ t = fromDescListWithKey f downSortedKxs
1344
1319
1345
1320
prop_fromDistinctDescList :: [(Int , A )] -> Property
1346
1321
prop_fromDistinctDescList kxs =
1347
1322
valid t .&&.
1348
1323
toList t === reverse nubDownSortedKxs
1349
1324
where
1350
- nubDownSortedKxs = [(k, NE. head xs) | (k,xs) <- groupByK (List. sortBy (comparing (Down . fst )) kxs)]
1325
+ nubDownSortedKxs =
1326
+ List. map NE. head $
1327
+ NE. groupBy ((==) `on` fst ) $
1328
+ List. sortBy (comparing (Down . fst )) kxs
1351
1329
t = fromDistinctDescList nubDownSortedKxs
1352
1330
1353
1331
prop_ascDescList :: [Int ] -> Bool
@@ -1361,51 +1339,43 @@ prop_fromList xs
1361
1339
t == List. foldr (uncurry insert) empty (zip xs xs)
1362
1340
where sort_xs = sort xs
1363
1341
1364
- -- For a list with keys in increasing order (sortedKxs), the sequence we get from fromAscList
1365
- -- should be the same as the sequence we get if we group by the key and take the last key-value pair
1366
- -- in each group.
1367
1342
prop_fromAscList :: [(Int , A )] -> Property
1368
1343
prop_fromAscList kxs =
1369
1344
valid t .&&.
1370
- toList t === nubLastSortedXs
1345
+ t === fromList sortedKxs
1371
1346
where
1372
1347
sortedKxs = List. sortBy (comparing fst ) kxs
1373
- nubLastSortedXs = [(k, NE. last xs) | (k,xs) <- groupByK sortedKxs]
1374
1348
t = fromAscList sortedKxs
1375
1349
1376
- -- For a list with keys in increasing order (sortedKxs), the sequence we get from fromAscListWith f
1377
- -- should be the same as the the sequence we get if we group by the key and fold all the values for
1378
- -- a key with f. fromAscListWith applies f as `f newv oldv`.
1379
1350
prop_fromAscListWith :: [(Int , A )] -> Property
1380
1351
prop_fromAscListWith kys =
1381
1352
valid t .&&.
1382
- toList t === foldedSortedKxs
1353
+ t === fromListWith (:*) sortedKxs
1383
1354
where
1384
1355
kxs = [(k, Inj y) | (k,y) <- kys]
1385
1356
sortedKxs = List. sortBy (comparing fst ) kxs
1386
- foldedSortedKxs = [(k, Foldable. foldl1 (flip (:*) ) x) | (k,x) <- groupByK sortedKxs]
1387
1357
t = fromAscListWith (:*) sortedKxs
1388
1358
1389
- -- For a list with keys in increasing order (sortedKxs), the sequence we get from
1390
- -- fromAscListWithKey f should be the same as the the sequence we get if we group by the key and
1391
- -- fold all the values for a key with f. fromAscListWithKey applies f as `f key newv oldv`.
1392
1359
prop_fromAscListWithKey :: [(Int , A )] -> Property
1393
1360
prop_fromAscListWithKey kys =
1394
1361
valid t .&&.
1395
- toList t === foldedSortedKxs
1362
+ t === fromListWithKey f sortedKxs
1396
1363
where
1397
1364
kxs = [(k, Inj (Left y)) | (k,y) <- kys]
1398
1365
sortedKxs = List. sortBy (comparing fst ) kxs
1399
- foldedSortedKxs = [ (k, Foldable. foldl1 ( \ acc ( Inj (Left x)) -> Inj (Right (k,x)) :* acc) xs)
1400
- | (k,xs) <- groupByK sortedKxs ]
1401
- t = fromAscListWithKey ( \ k ( Inj ( Left x)) acc -> Inj ( Right (k,x)) :* acc) sortedKxs
1366
+ f k ( Inj (Left x)) acc = Inj (Right (k,x)) :* acc
1367
+ f _ _ _ = error " prop_fromAscListWithKey "
1368
+ t = fromAscListWithKey f sortedKxs
1402
1369
1403
1370
prop_fromDistinctAscList :: [(Int , A )] -> Property
1404
1371
prop_fromDistinctAscList kxs =
1405
1372
valid t .&&.
1406
1373
toList t === nubSortedKxs
1407
1374
where
1408
- nubSortedKxs = [(k, NE. head xs) | (k,xs) <- groupByK (List. sortBy (comparing fst ) kxs)]
1375
+ nubSortedKxs =
1376
+ List. map NE. head $
1377
+ NE. groupBy ((==) `on` fst ) $
1378
+ List. sortBy (comparing fst ) kxs
1409
1379
t = fromDistinctAscList nubSortedKxs
1410
1380
1411
1381
----------------------------------------------------------------
0 commit comments