on baac813 help sign in
patch/src/Reflex/FunctorMaybe.hs
43 lines 1.3 kB history raw
1-- | This module defines the FunctorMaybe class, which extends Functors with the
2-- ability to delete values.
3module Reflex.FunctorMaybe
4 ( FunctorMaybe (..)
5 ) where
7import Data.IntMap (IntMap)
8import qualified Data.IntMap as IntMap
9import Data.Map (Map)
10import qualified Data.Map as Map
11import Data.Maybe
13--TODO: See if there's a better class in the standard libraries already
15-- | A class for values that combines filtering and mapping using 'Maybe'.
16-- Morally, @'FunctorMaybe' ~ KleisliFunctor 'Maybe'@. Also similar is the
17-- @Witherable@ typeclass, but it requires @Foldable f@ and @Traverable f@,
18-- and e.g. 'Event' is instance of neither.
19--
20-- A definition of 'fmapMaybe' must satisfy the following laws:
21--
22-- [/identity/]
23-- @'fmapMaybe' 'Just' ≡ 'id'@
24--
25-- [/composition/]
26-- @'fmapMaybe' (f <=< g) ≡ 'fmapMaybe' f . 'fmapMaybe' g@
27class FunctorMaybe f where
28 -- | Combined mapping and filtering function.
29 fmapMaybe :: (a -> Maybe b) -> f a -> f b
31-- | @fmapMaybe = (=<<)
32instance FunctorMaybe Maybe where
33 fmapMaybe = (=<<)
35-- | @fmapMaybe = mapMaybe@
36instance FunctorMaybe [] where
37 fmapMaybe = mapMaybe
39instance FunctorMaybe (Map k) where
40 fmapMaybe = Map.mapMaybe
42instance FunctorMaybe IntMap where
43 fmapMaybe = IntMap.mapMaybe