From be474de1def8aa2c1057340c7e1ad614718390e5 Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Fri, 12 Jul 2024 13:55:14 -0400 Subject: [PATCH] added filterWithKey to IsMap --- mono-traversable/ChangeLog.md | 5 +++++ mono-traversable/package.yaml | 2 +- mono-traversable/src/Data/Containers.hs | 16 ++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/mono-traversable/ChangeLog.md b/mono-traversable/ChangeLog.md index 4f0b449d..291498c7 100644 --- a/mono-traversable/ChangeLog.md +++ b/mono-traversable/ChangeLog.md @@ -1,5 +1,10 @@ # ChangeLog for mono-traversable +## 1.0.19.0 + +* Added `filterWithKey` to class `IsMap` + [#231](https://github.com/snoyberg/mono-traversable/pull/231) + ## 1.0.18.0 * Added MonoPointed instance for text Builder diff --git a/mono-traversable/package.yaml b/mono-traversable/package.yaml index d8a68e4f..0bae5d63 100644 --- a/mono-traversable/package.yaml +++ b/mono-traversable/package.yaml @@ -1,5 +1,5 @@ name: mono-traversable -version: 1.0.18.0 +version: 1.0.19.0 synopsis: Type classes for mapping, folding, and traversing monomorphic containers description: Please see the README at category: Data diff --git a/mono-traversable/src/Data/Containers.hs b/mono-traversable/src/Data/Containers.hs index 31322e8a..04ad1f75 100644 --- a/mono-traversable/src/Data/Containers.hs +++ b/mono-traversable/src/Data/Containers.hs @@ -548,8 +548,14 @@ class (MonoTraversable map, SetContainer map) => IsMap map where -- | Filter values in a map. -- -- @since 1.0.9.0 - filterMap :: IsMap map => (MapValue map -> Bool) -> map -> map - filterMap p = mapFromList . filter (p . snd) . mapToList + filterMap :: (MapValue map -> Bool) -> map -> map + filterMap = filterWithKey . const + + -- | Filter a map with keys and values. + -- + -- @since 1.0.19.0 + filterWithKey :: (ContainerKey map -> MapValue map -> Bool) -> map -> map + filterWithKey p = mapFromList . filter (uncurry p) . mapToList -- | This instance uses the functions from "Data.Map.Strict". instance Ord key => IsMap (Map.Map key value) where @@ -599,6 +605,8 @@ instance Ord key => IsMap (Map.Map key value) where {-# INLINE omapKeysWith #-} filterMap = Map.filter {-# INLINE filterMap #-} + filterWithKey = Map.filterWithKey + {-# INLINE filterWithKey #-} -- | This instance uses the functions from "Data.HashMap.Strict". instance (Eq key, Hashable key) => IsMap (HashMap.HashMap key value) where @@ -636,6 +644,8 @@ instance (Eq key, Hashable key) => IsMap (HashMap.HashMap key value) where --mapKeysWith = HashMap.mapKeysWith filterMap = HashMap.filter {-# INLINE filterMap #-} + filterWithKey = HashMap.filterWithKey + {-# INLINE filterWithKey #-} -- | This instance uses the functions from "Data.IntMap.Strict". instance IsMap (IntMap.IntMap value) where @@ -684,6 +694,8 @@ instance IsMap (IntMap.IntMap value) where {-# INLINE omapKeysWith #-} filterMap = IntMap.filter {-# INLINE filterMap #-} + filterWithKey = IntMap.filterWithKey + {-# INLINE filterWithKey #-} instance Eq key => IsMap [(key, value)] where type MapValue [(key, value)] = value