{-# LANGUAGE ScopedTypeVariables #-}
module Data.List.PointedList.Extras
( filterr
, catMaybesr
) where
import Control.Monad (guard)
import Data.List.PointedList (PointedList(..))
import qualified Data.Maybe as M (catMaybes)
filterr :: (a -> Bool) -> PointedList a -> Maybe (PointedList a)
filterr filt pl = catMaybesr $ fmap (\a -> guard (filt a) >> Just a) pl
catMaybesr :: forall a . PointedList (Maybe a) -> Maybe (PointedList a)
catMaybesr (PointedList mls mf mrs) = case mf of
Nothing -> shiftFocus rs ss
Just f -> pure $ PointedList rs f ss
where
rs, ss :: [a]
rs = M.catMaybes mls
ss = M.catMaybes mrs
shiftFocus :: [a] -> [a] -> Maybe (PointedList a)
shiftFocus [] (f:rs) = pure $ PointedList [] f rs
shiftFocus (f:ls) [] = pure $ PointedList ls f []
shiftFocus _ _ = Nothing