[commit: ghc] master: base: Override Foldable.{toList, length} for NonEmpty (94d2cce)

git at git.haskell.org git at git.haskell.org
Fri Dec 23 22:48:26 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/94d2cce6742235d36efb71cf704acd1327a68481/ghc

>---------------------------------------------------------------

commit 94d2cce6742235d36efb71cf704acd1327a68481
Author: Artyom <yom at artyom.me>
Date:   Fri Dec 23 14:36:55 2016 -0500

    base: Override Foldable.{toList,length} for NonEmpty
    
    Previously the Foldable instance for NonEmpty used default
    implementations for toList and length.
    
    I assume that the existing implementations
    (i.e. Data.List.NonEmpty.{toList,length}) are better than
    the default ones, and frankly can't see a good reason why they
    might be worse – but if they are, instead of this commit
    we'd have to switch Data.List.NonEmpty.{toList,length}
    to use Foldable.
    
    Reviewers: austin, hvr, bgamari
    
    Reviewed By: bgamari
    
    Subscribers: int-index, thomie
    
    Differential Revision: https://phabricator.haskell.org/D2882


>---------------------------------------------------------------

94d2cce6742235d36efb71cf704acd1327a68481
 libraries/base/Data/List/NonEmpty.hs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libraries/base/Data/List/NonEmpty.hs b/libraries/base/Data/List/NonEmpty.hs
index b4da532..c5f6169 100644
--- a/libraries/base/Data/List/NonEmpty.hs
+++ b/libraries/base/Data/List/NonEmpty.hs
@@ -228,6 +228,8 @@ instance Foldable NonEmpty where
   foldl1 f ~(a :| as) = foldl f a as
   foldMap f ~(a :| as) = f a `mappend` foldMap f as
   fold ~(m :| ms) = m `mappend` fold ms
+  length = length
+  toList = toList
 
 -- | Extract the first element of the stream.
 head :: NonEmpty a -> a
@@ -507,8 +509,8 @@ nubBy eq (a :| as) = a :| List.nubBy eq (List.filter (\b -> not (eq a b)) as)
 -- > transpose . transpose /= id
 transpose :: NonEmpty (NonEmpty a) -> NonEmpty (NonEmpty a)
 transpose = fmap fromList
-          . fromList . List.transpose . Foldable.toList
-          . fmap Foldable.toList
+          . fromList . List.transpose . toList
+          . fmap toList
 
 -- | 'sortBy' for 'NonEmpty', behaves the same as 'Data.List.sortBy'
 sortBy :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a



More information about the ghc-commits mailing list