More Data.Set and Data.Map operations

Jan-Willem Maessen jmaessen at alum.mit.edu
Sun Aug 7 19:22:51 UTC 2016


On Sat, Aug 6, 2016 at 11:15 PM, David Feuer <david.feuer at gmail.com> wrote:

> Set:
>
> filterIncreasing :: Ord a => (a -> Bool) -> Set a -> Set a
> filterDecreasing :: Ord a => (a -> Bool) -> Set a -> Set a
> partitionIncreasing :: Ord a => (a -> Bool) -> Set a -> (Set a, Set a)
>
> Map:
>
> filterKeysIncreasing :: Ord k => (k -> Bool) -> Map k a -> Map k a
> filterKeysDecreasing :: Ord k => (k -> Bool) -> Map k a -> Map k a
> partitionKeysIncreasing :: Ord k => (k -> Bool) -> Map k a -> (Map k a,
> Map k a)
>

Are there good examples of increasing / decreasing predicates that don't
embed a split element?  Because split already gives the results you want.


> alterMany :: Ord k => (a -> Maybe b -> Maybe b) -> Map k a -> Map k b ->
> Map k b
>


> unionWithMapping :: Ord k => (a -> c) -> (b -> c) -> (a -> b -> c) ->
> Map k a -> Map k b -> Map k c
>
> unionWithKeyMapping :: Ord k => (k -> a -> c) -> (k -> b -> c) -> (k
> -> a -> b -> c)
> -> Map k a -> Map k b -> Map k c
>

Note that these three are each one-liners given mergeWithKey:

alterMany c = mergeWithKey (\_ a b -> c a (Just b)) (mapMaybe (\a -> c a
Nothing)) (const empty)
unionWithMapping f g c = mergeWithKey (\_ a b -> Just (c a b)) (fmap f)
(fmap g)
unionWithKeyMapping f g c = mergeWithKey (\k a b -> Just (c k a b))
(mapWithKey f) (mapWithKey g)

There's a pretty large combinatorial space of these functions – I'm not
sure Data.Map and Data.Set should fill that entire space with
individually-named functions.

The applicative versions suggest there might want to be an
applicative-friendly version of mergeWithKey, but again it's not obvious we
need the 32767 different combinations of individual functions.


> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160807/9ea98cf9/attachment.html>


More information about the Libraries mailing list