on master help sign in
Implement Foldable and Traversable for Result and IResult
commit360d51135d47a72dbfdcf2c30a5992e8045597a6 authorBryan O'Sullivan <bos@serpentine.com> dateFri 10 Jul 2015, 17:40 (11 years ago) parent09b2ddc
1 file changed+30
Data/Aeson/Types/Internal.hs+30
Data/Aeson/Types/Internal.hs+30
@@ -58,6 +58,7 @@ import Control.DeepSeq (NFData(..))
5858import Data.ByteString.Builder (Builder, char7, toLazyByteString)
5959import Data.Char (toLower, isUpper)
6060import Data.Scientific (Scientific)
61+import Data.Foldable (Foldable(..))
6162import Data.Hashable (Hashable(..))
6263import Data.Data (Data)
6364import Data.HashMap.Strict (HashMap)
@@ -66,6 +67,7 @@ import Data.String (IsString(..))
6667import Data.Text (Text, pack, unpack)
6768import Data.Time (UTCTime)
6869import Data.Time.Format (FormatTime)
70+import Data.Traversable (Traversable(..))
6971import Data.Typeable (Typeable)
7072import Data.Vector (Vector)
7173import qualified Data.HashMap.Strict as H
@@ -186,6 +188,34 @@ instance Monoid (Result a) where
186188 mappend = mplus
187189 {-# INLINE mappend #-}
188190
191+instance Foldable IResult where
192+ foldMap _ (IError _ _) = mempty
193+ foldMap f (ISuccess y) = f y
194+ {-# INLINE foldMap #-}
195+
196+ foldr _ z (IError _ _) = z
197+ foldr f z (ISuccess y) = f y z
198+ {-# INLINE foldr #-}
199+
200+instance Foldable Result where
201+ foldMap _ (Error _) = mempty
202+ foldMap f (Success y) = f y
203+ {-# INLINE foldMap #-}
204+
205+ foldr _ z (Error _) = z
206+ foldr f z (Success y) = f y z
207+ {-# INLINE foldr #-}
208+
209+instance Traversable IResult where
210+ traverse _ (IError path err) = pure (IError path err)
211+ traverse f (ISuccess a) = ISuccess <$> f a
212+ {-# INLINE traverse #-}
213+
214+instance Traversable Result where
215+ traverse _ (Error err) = pure (Error err)
216+ traverse f (Success a) = Success <$> f a
217+ {-# INLINE traverse #-}
218+
189219-- | Failure continuation.
190220type Failure f r = JSONPath -> String -> f r
191221-- | Success continuation.