Skip to content

Commit cfebb6d

Browse files
authored
Merge pull request #11 from garyb/mapmaybe-more-filters
Add mapMaybe functions for Map and Set types, filter for Set types
2 parents 5b21750 + 9807aaf commit cfebb6d

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/Data/Map.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Data.Map
55

66
import Prelude
77

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

src/Data/Map/Internal.purs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ module Data.Map.Internal
3838
, filterWithKey
3939
, filterKeys
4040
, filter
41+
, mapMaybeWithKey
42+
, mapMaybe
4143
) where
4244

4345
import Prelude
4446

4547
import Data.Eq (class Eq1)
4648
import Data.Foldable (foldl, foldMap, foldr, class Foldable)
47-
import Data.FoldableWithIndex (class FoldableWithIndex)
49+
import Data.FoldableWithIndex (class FoldableWithIndex, foldrWithIndex)
4850
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
4951
import Data.List (List(..), (:), length, nub)
5052
import Data.List.Lazy as LL
@@ -644,3 +646,13 @@ filterKeys predicate = filterWithKey $ const <<< predicate
644646
-- | on the value fails to hold.
645647
filter :: forall k v. Ord k => (v -> Boolean) -> Map k v -> Map k v
646648
filter predicate = filterWithKey $ const predicate
649+
650+
-- | Applies a function to each key/value pair in a map, discarding entries
651+
-- | where the function returns `Nothing`.
652+
mapMaybeWithKey :: forall k a b. Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b
653+
mapMaybeWithKey f = foldrWithIndex (\k a acc → maybe acc (\b -> insert k b acc) (f k a)) empty
654+
655+
-- | Applies a function to each value in a map, discarding entries where the
656+
-- | function returns `Nothing`.
657+
mapMaybe :: forall k a b. Ord k => (a -> Maybe b) -> Map k a -> Map k b
658+
mapMaybe = mapMaybeWithKey <<< const

src/Data/Set.purs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module Data.Set
2424
, subset
2525
, properSubset
2626
, intersection
27+
, filter
28+
, mapMaybe
2729
) where
2830

2931
import Prelude hiding (map)
@@ -39,7 +41,7 @@ import Data.Foldable (class Foldable, foldMap, foldl, foldr)
3941
import Data.List (List)
4042
import Data.List as List
4143
import Data.Map.Internal as M
42-
import Data.Maybe (Maybe)
44+
import Data.Maybe (Maybe, maybe)
4345
import Data.Ord (class Ord1)
4446
import Data.Unfoldable (class Unfoldable)
4547
import Partial.Unsafe (unsafePartial)
@@ -178,3 +180,13 @@ intersection s1 s2 = fromFoldable (ST.run (STArray.empty >>= intersect >>= STArr
178180
LT -> pure $ Loop {a: l + 1, b: r}
179181
GT -> pure $ Loop {a: l, b: r + 1}
180182
else pure $ Done acc
183+
184+
-- | Filter out those values of a set for which a predicate on the value fails
185+
-- | to hold.
186+
filter :: forall a. Ord a => (a -> Boolean) -> Set a -> Set a
187+
filter f (Set s) = Set (M.filterWithKey (\k _ -> f k) s)
188+
189+
-- | Applies a function to each value in a set, discarding entries where the
190+
-- | function returns `Nothing`.
191+
mapMaybe :: forall a b. Ord b => (a -> Maybe b) -> Set a -> Set b
192+
mapMaybe f = foldr (\a acc -> maybe acc (\b -> insert b acc) (f a)) empty

src/Data/Set/NonEmpty.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module Data.Set.NonEmpty
2020
, subset
2121
, properSubset
2222
, intersection
23+
, filter
24+
, mapMaybe
2325
) where
2426

2527
import Prelude hiding (map)
@@ -147,3 +149,13 @@ properSubset (NonEmptySet s1) (NonEmptySet s2) = Set.properSubset s1 s2
147149
-- | if the sets are disjoint.
148150
intersection :: forall a. Ord a => NonEmptySet a -> NonEmptySet a -> Maybe (NonEmptySet a)
149151
intersection (NonEmptySet s1) (NonEmptySet s2) = fromSet (Set.intersection s1 s2)
152+
153+
-- | Filter out those values of a set for which a predicate on the value fails
154+
-- | to hold.
155+
filter :: forall a. Ord a => (a -> Boolean) -> NonEmptySet a -> Set a
156+
filter f (NonEmptySet s) = Set.filter f s
157+
158+
-- | Applies a function to each value in a set, discarding entries where the
159+
-- | function returns `Nothing`.
160+
mapMaybe :: forall a b. Ord b => (a -> Maybe b) -> NonEmptySet a -> Set b
161+
mapMaybe f (NonEmptySet s) = Set.mapMaybe f s

0 commit comments

Comments
 (0)