on 032e6b2 help sign in
patch/src/Reflex/FunctorMaybe.hs
54 lines 1.5 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE StandaloneDeriving #-}
5-- | This module defines the FunctorMaybe class, which extends Functors with the
6-- ability to delete values.
7module Reflex.FunctorMaybe
8 ( FunctorMaybe (..)
9 ) where
11import Data.IntMap (IntMap)
12import qualified Data.IntMap as IntMap
13import Data.Map (Map)
14import qualified Data.Map as Map
15import Data.Maybe
16#if MIN_VERSION_base(4,9,0)
17import Data.Semigroup (Option(..))
18#endif
20--TODO: See if there's a better class in the standard libraries already
22-- | A class for values that combines filtering and mapping using 'Maybe'.
23-- Morally, @'FunctorMaybe' ~ KleisliFunctor 'Maybe'@. Also similar is the
24-- @Witherable@ typeclass, but it requires @'Foldable' f@ and @'Traversable' f@,
25-- and e.g. 'Event' is instance of neither.
26--
27-- A definition of 'fmapMaybe' must satisfy the following laws:
28--
29-- [/identity/]
30-- @'fmapMaybe' 'Just' ≡ 'id'@
31--
32-- [/composition/]
33-- @'fmapMaybe' (f <=< g) ≡ 'fmapMaybe' f . 'fmapMaybe' g@
34class FunctorMaybe f where
35 -- | Combined mapping and filtering function.
36 fmapMaybe :: (a -> Maybe b) -> f a -> f b
38-- | @fmapMaybe = (=<<)
39instance FunctorMaybe Maybe where
40 fmapMaybe = (=<<)
42#if MIN_VERSION_base(4,9,0)
43deriving instance FunctorMaybe Option
44#endif
46-- | @fmapMaybe = mapMaybe@
47instance FunctorMaybe [] where
48 fmapMaybe = mapMaybe
50instance FunctorMaybe (Map k) where
51 fmapMaybe = Map.mapMaybe
53instance FunctorMaybe IntMap where
54 fmapMaybe = IntMap.mapMaybe