Search code examples
haskellsetoption-type

Haskell: catMaybe for Data.Set?


how would you implement a catMaybes for Data.Set ?

I came up with:

import qualified Data.Set as Set
import qualified Data.Maybe as Maybe
setCatMaybes a = Set.map Maybe.fromJust . Set.delete Nothing $ a

fnord = Set.fromList [Nothing, Just 41, Just 43, Just 47]

then i get the following

setCatMaybes fnord == fromList [41,43,47]

Solution

  • I think the solution you already have is probably the best one. Along the lines of John's solution, here's a fairly short one:

    setCatMaybes :: Ord a => Set.Set (Maybe a) -> Set.Set a
    setCatMaybes s = Set.fromAscList [x | Just x <- Set.toAscList s]
    

    Or here's a longer one, that may be faster:

    setCatMaybes2 :: Ord a => Set.Set (Maybe a) -> Set.Set a
    setCatMaybes2 s
      | Set.null s = Set.empty
      | otherwise  = Set.mapMonotonic Maybe.fromJust $ case Set.deleteFindMin s of
                       (Nothing, s') ->  s'
                       _ -> s