Skip to content

Commit

Permalink
Merge pull request #11 from garyb/mapmaybe-more-filters
Browse files Browse the repository at this point in the history
Add mapMaybe functions for Map and Set types, filter for Set types
  • Loading branch information
garyb committed Oct 5, 2018
2 parents 5b21750 + 9807aaf commit cfebb6d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 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, insert, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, difference, update, values)
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)
import Data.Set (Set)
import Unsafe.Coerce (unsafeCoerce)

Expand Down
14 changes: 13 additions & 1 deletion src/Data/Map/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ module Data.Map.Internal
, filterWithKey
, filterKeys
, filter
, mapMaybeWithKey
, mapMaybe
) where

import Prelude

import Data.Eq (class Eq1)
import Data.Foldable (foldl, foldMap, foldr, class Foldable)
import Data.FoldableWithIndex (class FoldableWithIndex)
import Data.FoldableWithIndex (class FoldableWithIndex, foldrWithIndex)
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
import Data.List (List(..), (:), length, nub)
import Data.List.Lazy as LL
Expand Down Expand Up @@ -644,3 +646,13 @@ filterKeys predicate = filterWithKey $ const <<< predicate
-- | on the value fails to hold.
filter :: forall k v. Ord k => (v -> Boolean) -> Map k v -> Map k v
filter predicate = filterWithKey $ const predicate

-- | Applies a function to each key/value pair in a map, discarding entries
-- | where the function returns `Nothing`.
mapMaybeWithKey :: forall k a b. Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey f = foldrWithIndex (\k a acc → maybe acc (\b -> insert k b acc) (f k a)) empty

-- | Applies a function to each value in a map, discarding entries where the
-- | function returns `Nothing`.
mapMaybe :: forall k a b. Ord k => (a -> Maybe b) -> Map k a -> Map k b
mapMaybe = mapMaybeWithKey <<< const
14 changes: 13 additions & 1 deletion src/Data/Set.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module Data.Set
, subset
, properSubset
, intersection
, filter
, mapMaybe
) where

import Prelude hiding (map)
Expand All @@ -39,7 +41,7 @@ import Data.Foldable (class Foldable, foldMap, foldl, foldr)
import Data.List (List)
import Data.List as List
import Data.Map.Internal as M
import Data.Maybe (Maybe)
import Data.Maybe (Maybe, maybe)
import Data.Ord (class Ord1)
import Data.Unfoldable (class Unfoldable)
import Partial.Unsafe (unsafePartial)
Expand Down Expand Up @@ -178,3 +180,13 @@ intersection s1 s2 = fromFoldable (ST.run (STArray.empty >>= intersect >>= STArr
LT -> pure $ Loop {a: l + 1, b: r}
GT -> pure $ Loop {a: l, b: r + 1}
else pure $ Done acc

-- | Filter out those values of a set for which a predicate on the value fails
-- | to hold.
filter :: forall a. Ord a => (a -> Boolean) -> Set a -> Set a
filter f (Set s) = Set (M.filterWithKey (\k _ -> f k) s)

-- | Applies a function to each value in a set, discarding entries where the
-- | 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
12 changes: 12 additions & 0 deletions src/Data/Set/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Data.Set.NonEmpty
, subset
, properSubset
, intersection
, filter
, mapMaybe
) where

import Prelude hiding (map)
Expand Down Expand Up @@ -147,3 +149,13 @@ properSubset (NonEmptySet s1) (NonEmptySet s2) = Set.properSubset s1 s2
-- | if the sets are disjoint.
intersection :: forall a. Ord a => NonEmptySet a -> NonEmptySet a -> Maybe (NonEmptySet a)
intersection (NonEmptySet s1) (NonEmptySet s2) = fromSet (Set.intersection s1 s2)

-- | Filter out those values of a set for which a predicate on the value fails
-- | to hold.
filter :: forall a. Ord a => (a -> Boolean) -> NonEmptySet a -> Set a
filter f (NonEmptySet s) = Set.filter f s

-- | Applies a function to each value in a set, discarding entries where the
-- | function returns `Nothing`.
mapMaybe :: forall a b. Ord b => (a -> Maybe b) -> NonEmptySet a -> Set b
mapMaybe f (NonEmptySet s) = Set.mapMaybe f s

0 comments on commit cfebb6d

Please sign in to comment.