Skip to content

Commit 296a135

Browse files
committed
Utils: implement norep in terms of norepBy
1 parent f243ba5 commit 296a135

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lib/Utils.hs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE RankNTypes #-}
33

4+
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
5+
46
-- | Internal module for utilities used in the implementation.
57

68
module Utils (module Utils, module X) where
@@ -44,24 +46,22 @@ over l f o = runIdentity $ l (Identity . f) o
4446
#endif
4547

4648
-- | After 'sort' or 'sortBy' the use of 'nub' or 'nubBy' can be replaced by 'norep' or 'norepBy'.
47-
norep :: (Eq a) => [a]->[a]
48-
norep [] = []
49-
norep x@[_] = x
50-
norep (a:bs@(c:cs)) | a==c = norep (a:cs)
51-
| otherwise = a:norep bs
49+
norep :: Eq a => [a] -> [a]
50+
norep = norepBy (==)
5251

5352
-- | After 'sort' or 'sortBy' the use of 'nub' or 'nubBy' can be replaced by 'norep' or 'norepBy'.
5453
norepBy :: (a -> a -> Bool) -> [a] -> [a]
55-
norepBy _ [] = []
56-
norepBy _ x@[_] = x
57-
norepBy eqF (a:bs@(c:cs)) | a `eqF` c = norepBy eqF (a:cs)
58-
| otherwise = a:norepBy eqF bs
54+
norepBy _ [] = []
55+
norepBy eq (a:as) = loop a as
56+
where
57+
loop a [] = [a]
58+
loop a (b:bs) = (if a `eq` b then id else (a:)) $ loop b bs
5959

60-
mapFst :: (Functor f) => (t -> t2) -> f (t, t1) -> f (t2, t1)
61-
mapFst f = fmap (\ (a,b) -> (f a,b))
60+
mapFst :: Functor f => (t1 -> t2) -> f (t1, t) -> f (t2, t)
61+
mapFst f = fmap $ \ (a, b) -> (f a, b)
6262

63-
mapSnd :: (Functor f) => (t1 -> t2) -> f (t, t1) -> f (t, t2)
64-
mapSnd f = fmap (\ (a,b) -> (a,f b))
63+
mapSnd :: Functor f => (t1 -> t2) -> f (t, t1) -> f (t, t2)
64+
mapSnd f = fmap $ \ (a, b) -> (a, f b)
6565

6666
fst3 :: (a,b,c) -> a
6767
fst3 (x,_,_) = x

0 commit comments

Comments
 (0)