PROPOSAL: Add Data.Set.mapMaybe

Hi there. I recently find myself missing Data.Set.mapMaybe. This function is implemented for Data.Map.Map, but is strangely missing from Data.Set.mapMaybe. I believe it could be implemented using Data.Foldable, but not as efficiently. The obvious implementation would be: mapMaybe :: (Ord a, Ord b) => (a -> Maybe b) -> Set a -> Set b mapMaybe f Tip = Tip mapMaybe f (Bin _ x l r) = case f x of Nothing -> merge (mapMaybe f l) (mapMaybe f r) Just y -> union y (mapMaybe f l) (mapMaybe f r) Any comments? Preliminary deadline: Nov 21, 2007 (3 weeks)

On Wed, 2007-10-31 at 14:29 +0100, Thomas Schilling wrote:
Hi there.
I recently find myself missing Data.Set.mapMaybe. This function is implemented for Data.Map.Map, but is strangely missing from Data.Set.mapMaybe. I believe it could be implemented using Data.Foldable, but not as efficiently. The obvious implementation would be:
mapMaybe :: (Ord a, Ord b) => (a -> Maybe b) -> Set a -> Set b mapMaybe f Tip = Tip mapMaybe f (Bin _ x l r) = case f x of Nothing -> merge (mapMaybe f l) (mapMaybe f r) Just y -> union y (mapMaybe f l) (mapMaybe f r)
Any comments?
D'oh! This is wrong. Sorry, for the noise.

On Wed, 2007-10-31 at 14:38 +0100, Thomas Schilling wrote:
On Wed, 2007-10-31 at 14:29 +0100, Thomas Schilling wrote:
Hi there.
I recently find myself missing Data.Set.mapMaybe. This function is implemented for Data.Map.Map, but is strangely missing from Data.Set.mapMaybe. I believe it could be implemented using Data.Foldable, but not as efficiently. The obvious implementation would be:
mapMaybe :: (Ord a, Ord b) => (a -> Maybe b) -> Set a -> Set b mapMaybe f Tip = Tip mapMaybe f (Bin _ x l r) = case f x of Nothing -> merge (mapMaybe f l) (mapMaybe f r) Just y -> union y (mapMaybe f l) (mapMaybe f r)
Any comments?
D'oh! This is wrong. Sorry, for the noise.
Should be: mapMaybe :: (Ord a, Ord b) => (a -> Maybe b) -> Set a -> Set b mapMaybe f = foldl' g Set.empty . Set.toList where g s a = case f a of Nothing -> s Just b -> Set.insert b s testMapMaybe = mapMaybe f (Set.fromList [(-4),(-3)..5]) where f x | odd x = Nothing | otherwise = Just (-(x * x))

nominolo:
On Wed, 2007-10-31 at 14:38 +0100, Thomas Schilling wrote:
On Wed, 2007-10-31 at 14:29 +0100, Thomas Schilling wrote:
Hi there.
I recently find myself missing Data.Set.mapMaybe. This function is implemented for Data.Map.Map, but is strangely missing from Data.Set.mapMaybe. I believe it could be implemented using Data.Foldable, but not as efficiently. The obvious implementation would be:
mapMaybe :: (Ord a, Ord b) => (a -> Maybe b) -> Set a -> Set b mapMaybe f Tip = Tip mapMaybe f (Bin _ x l r) = case f x of Nothing -> merge (mapMaybe f l) (mapMaybe f r) Just y -> union y (mapMaybe f l) (mapMaybe f r)
Any comments?
D'oh! This is wrong. Sorry, for the noise.
Should be:
mapMaybe :: (Ord a, Ord b) => (a -> Maybe b) -> Set a -> Set b mapMaybe f = foldl' g Set.empty . Set.toList where g s a = case f a of Nothing -> s Just b -> Set.insert b s
testMapMaybe = mapMaybe f (Set.fromList [(-4),(-3)..5]) where f x | odd x = Nothing | otherwise = Just (-(x * x))
Looks good. Did you open up a libraries ticket? -- Don

On Wed, 2007-10-31 at 11:01 -0700, Don Stewart wrote:
Looks good. Did you open up a libraries ticket?
participants (2)
-
Don Stewart
-
Thomas Schilling