Expanding my comment above - this approach is not too different from the code published by OP.
First, a -> Maybe a determine how to enable the function a -> Maybe a in a -> a , replacing the input for Nothing .
totalize :: (a -> Maybe a) -> (a -> a) totalize fx = fromMaybe x (fx)
Then we use the following: we make each function "total" (which means no- Nothing s), end it as Endo , then we make a list of endomorphisms ( mconcat is a composition in Endo monoid).
e :: [a -> Maybe a] -> a -> a e = appEndo . mconcat . map (Endo . totalize)
or even (as suggested below)
e :: Foldable t => t (a -> Maybe a) -> a -> a e = appEndo . foldMap (Endo . totalize)
source share