X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FTraversable.hs;h=28fa761b98347955a6bbf64e5c4d089f2a01634f;hb=e74a10138daf9fd70888a1841bd6cc93b9cca9f2;hp=7ad399150127d92d96d7ce91d629afbd1214d269;hpb=1a2c75698b74fe40e3cd91854c3f6a64e1dac348;p=ghc-base.git diff --git a/Data/Traversable.hs b/Data/Traversable.hs index 7ad3991..28fa761 100644 --- a/Data/Traversable.hs +++ b/Data/Traversable.hs @@ -43,6 +43,14 @@ import Control.Applicative import Data.Foldable (Foldable()) import Data.Monoid (Monoid) +#if defined(__GLASGOW_HASKELL__) +import GHC.Arr +#elif defined(__HUGS__) +import Hugs.Array +#elif defined(__NHC__) +import Array +#endif + -- | Functors representing data structures that can be traversed from -- left to right. -- @@ -54,7 +62,7 @@ import Data.Monoid (Monoid) -- -- a suitable instance would be -- --- > instance Traversable Tree +-- > instance Traversable Tree where -- > traverse f Empty = pure Empty -- > traverse f (Leaf x) = Leaf <$> f x -- > traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r @@ -95,15 +103,19 @@ class (Functor t, Foldable t) => Traversable t where -- instances for Prelude types instance Traversable Maybe where - traverse f Nothing = pure Nothing + traverse _ Nothing = pure Nothing traverse f (Just x) = Just <$> f x instance Traversable [] where + {-# INLINE traverse #-} -- so that traverse can fuse traverse f = Prelude.foldr cons_f (pure []) where cons_f x ys = (:) <$> f x <*> ys mapM = Prelude.mapM +instance Ix i => Traversable (Array i) where + traverse f arr = listArray (bounds arr) `fmap` traverse f (elems arr) + -- general functions -- | 'for' is 'traverse' with its arguments flipped.