on 538e879 help sign in
patch/src/Reflex/FunctorMaybe.hs
33 lines 1.0 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.Maybe
9--TODO: See if there's a better class in the standard libraries already
11-- | A class for values that combines filtering and mapping using 'Maybe'.
12-- Morally, @'FunctorMaybe' ~ KleisliFunctor 'Maybe'@. Also similar is the
13-- @Witherable@ typeclass, but it requires @Foldable f@ and @Traverable f@,
14-- and e.g. 'Event' is instance of neither.
15--
16-- A definition of 'fmapMaybe' must satisfy the following laws:
17--
18-- [/identity/]
19-- @'fmapMaybe' 'Just' ≡ 'id'@
20--
21-- [/composition/]
22-- @'fmapMaybe' (f <=< g) ≡ 'fmapMaybe' f . 'fmapMaybe' g@
23class FunctorMaybe f where
24 -- | Combined mapping and filtering function.
25 fmapMaybe :: (a -> Maybe b) -> f a -> f b
27-- | @fmapMaybe = (=<<)
28instance FunctorMaybe Maybe where
29 fmapMaybe = (=<<)
31-- | @fmapMaybe f = catMaybes . fmap f@
32instance FunctorMaybe [] where
33 fmapMaybe f = catMaybes . fmap f