Skip to content

Commit 01642a0

Browse files
committed
Utils: implement norep in terms of norepBy
1 parent bf1e7d2 commit 01642a0

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
@@ -41,24 +43,22 @@ over l f o = runIdentity $ l (Identity . f) o
4143
#endif
4244

4345
-- | After 'sort' or 'sortBy' the use of 'nub' or 'nubBy' can be replaced by 'norep' or 'norepBy'.
44-
norep :: (Eq a) => [a]->[a]
45-
norep [] = []
46-
norep x@[_] = x
47-
norep (a:bs@(c:cs)) | a==c = norep (a:cs)
48-
| otherwise = a:norep bs
46+
norep :: Eq a => [a] -> [a]
47+
norep = norepBy (==)
4948

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

57-
mapFst :: (Functor f) => (t -> t2) -> f (t, t1) -> f (t2, t1)
58-
mapFst f = fmap (\ (a,b) -> (f a,b))
57+
mapFst :: Functor f => (t1 -> t2) -> f (t1, t) -> f (t2, t)
58+
mapFst f = fmap $ \ (a, b) -> (f a, b)
5959

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

6363
fst3 :: (a,b,c) -> a
6464
fst3 (x,_,_) = x

0 commit comments

Comments
 (0)