[commit: ghc] master: base: Enable listToMaybe to fuse via foldr/build (4c06ccb)
git at git.haskell.org
git at git.haskell.org
Thu Oct 26 00:03:11 UTC 2017
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/4c06ccb71737b77a8165e888ad75417a425549dd/ghc
>---------------------------------------------------------------
commit 4c06ccb71737b77a8165e888ad75417a425549dd
Author: Douglas Wilson <douglas.wilson at gmail.com>
Date: Wed Oct 25 19:05:30 2017 -0400
base: Enable listToMaybe to fuse via foldr/build
Test Plan: Consider whether this is a good idea.
Reviewers: austin, hvr, bgamari, nomeata
Reviewed By: bgamari, nomeata
Subscribers: nomeata, rwbarton, thomie
GHC Trac Issues: #14387
Differential Revision: https://phabricator.haskell.org/D4126
>---------------------------------------------------------------
4c06ccb71737b77a8165e888ad75417a425549dd
libraries/base/Data/Maybe.hs | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/libraries/base/Data/Maybe.hs b/libraries/base/Data/Maybe.hs
index d8aad53..5f5d5ac 100644
--- a/libraries/base/Data/Maybe.hs
+++ b/libraries/base/Data/Maybe.hs
@@ -228,9 +228,12 @@ maybeToList (Just x) = [x]
-- >>> maybeToList $ listToMaybe [1,2,3]
-- [1]
--
-listToMaybe :: [a] -> Maybe a
-listToMaybe [] = Nothing
-listToMaybe (a:_) = Just a
+listToMaybe :: [a] -> Maybe a
+listToMaybe = foldr (const . Just) Nothing
+{-# INLINE listToMaybe #-}
+-- We define listToMaybe using foldr so that it can fuse via the foldr/build
+-- rule. See #14387
+
-- | The 'catMaybes' function takes a list of 'Maybe's and returns
-- a list of all the 'Just' values.
More information about the ghc-commits
mailing list