Skip to content

Commit

Permalink
Add Map.catMaybes function (#25)
Browse files Browse the repository at this point in the history
* Add catMaybes function

* Add catMaybes to Set
  • Loading branch information
drewolson committed Nov 14, 2020
1 parent 201b304 commit 3857c6c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Data.Map

import Prelude

import Data.Map.Internal (Map, alter, checkValid, delete, empty, filter, filterKeys, filterWithKey, findMax, findMin, foldSubmap, fromFoldable, fromFoldableWith, fromFoldableWithIndex, insert, insertWith, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, intersection, intersectionWith, difference, update, values, mapMaybeWithKey, mapMaybe)
import Data.Map.Internal (Map, alter, catMaybes, checkValid, delete, empty, filter, filterKeys, filterWithKey, findMax, findMin, foldSubmap, fromFoldable, fromFoldableWith, fromFoldableWithIndex, insert, insertWith, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, intersection, intersectionWith, difference, update, values, mapMaybeWithKey, mapMaybe)
import Data.Set (Set)
import Unsafe.Coerce (unsafeCoerce)

Expand Down
6 changes: 6 additions & 0 deletions src/Data/Map/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module Data.Map.Internal
, filter
, mapMaybeWithKey
, mapMaybe
, catMaybes
) where

import Prelude
Expand Down Expand Up @@ -689,3 +690,8 @@ mapMaybeWithKey f = foldrWithIndex (\k a acc → maybe acc (\b -> insert k b acc
-- | function returns `Nothing`.
mapMaybe :: forall k a b. Ord k => (a -> Maybe b) -> Map k a -> Map k b
mapMaybe = mapMaybeWithKey <<< const

-- | Filter a map of optional values, keeping only the key/value pairs which
-- | contain a value, creating a new map.
catMaybes :: forall k v. Ord k => Map k (Maybe v) -> Map k v
catMaybes = mapMaybe identity
5 changes: 5 additions & 0 deletions src/Data/Set.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Data.Set
, intersection
, filter
, mapMaybe
, catMaybes
) where

import Prelude hiding (map)
Expand Down Expand Up @@ -190,3 +191,7 @@ filter f (Set s) = Set (M.filterWithKey (\k _ -> f k) s)
-- | function returns `Nothing`.
mapMaybe :: forall a b. Ord b => (a -> Maybe b) -> Set a -> Set b
mapMaybe f = foldr (\a acc -> maybe acc (\b -> insert b acc) (f a)) empty

-- | Filter a set of optional values, discarding values that contain `Nothing`
catMaybes :: forall a. Ord a => Set (Maybe a) -> Set a
catMaybes = mapMaybe identity
14 changes: 13 additions & 1 deletion test/Test/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Data.Tuple (Tuple(..), fst, uncurry)
import Effect (Effect)
import Effect.Console (log)
import Partial.Unsafe (unsafePartial)
import Test.QuickCheck ((<?>), (===), quickCheck, quickCheck')
import Test.QuickCheck ((<?>), (<=?), (===), quickCheck, quickCheck')
import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
import Test.QuickCheck.Gen (elements, oneOf)

Expand Down Expand Up @@ -370,3 +370,15 @@ mapTests = do
quickCheck \(TestMap m :: TestMap Int Int) ->
let outList = foldrWithIndex (\i a b -> (Tuple i a) : b) Nil m
in outList == sort outList

log "catMaybes creates a new map of size less than or equal to the original"
quickCheck \(TestMap m :: TestMap Int (Maybe Int)) -> do
let result = M.catMaybes m
M.size result <=? M.size m

log "catMaybes drops key/value pairs with Nothing values"
quickCheck \(TestMap m :: TestMap Int Int) -> do
let maybeMap = M.alter (const $ Just Nothing) 1 $ map Just m
let result = M.catMaybes maybeMap
let expected = M.delete 1 m
result === expected
6 changes: 6 additions & 0 deletions test/Test/Data/Set.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Test.Data.Set where

import Prelude

import Data.Maybe (Maybe(..))
import Data.Set (Set)
import Data.Set as S
import Effect (Effect)
Expand All @@ -25,3 +26,8 @@ setTests = do
s2 = S.fromFoldable [2,4,6,8,10]
s3 = S.fromFoldable [2,4]
assert $ S.intersection s1 s2 == s3

log "catMaybes - drops Nothing values"
do let s1 = S.fromFoldable [Just 1,Just 2,Just 3,Nothing]
s2 = S.fromFoldable [1,2,3]
assert $ S.catMaybes s1 == s2

0 comments on commit 3857c6c

Please sign in to comment.