Sebastiaan Joosten | 29 Jul 13:39 2013
Picon

Data.Map, unionWithMaybe

Dear libraries,

for Data.Map, I needed a "unionWithMaybe" function for my sparse system of linear equations (unionWithMaybe :: Ord k => (a -> a -> Maybe a) -> Map k a -> Map k a -> Map k a). My usage:
(.+.) = Map.unionWithMaybe (\a b->case a+b of {0->Nothing;s->Just s})
(I do not think Map.unionWithMaybe can be expressed in terms of other functions without loosing performance, so I recon it would be a nice addition to the library.)

I built this function myself by modifying the Data.Map implementation, but it would be nice to see it in the official version of Data.Map.
Here is the file including the modifications I made to it. Feel free to use it, I will agree to whatever license you need to make it public.

(PS: this is the first time I return modified source code to the maintainer, feel free to instruct me on how to do this in the future.)

Sincerely,

Sebastiaan J.C. Joosten


{-# OPTIONS_GHC -XNoBangPatterns -cpp -XStandaloneDeriving -XDeriveDataTypeable #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Map
-- Copyright   :  (c) Daan Leijen 2002
--                (c) Andriy Palamarchuk 2008
-- License     :  BSD-style
-- Maintainer  :  libraries <at> haskell.org
-- Stability   :  provisional
-- Portability :  portable
--
-- An efficient implementation of maps from keys to values (dictionaries).
--
(Continue reading)

Milan Straka | 29 Jul 16:10 2013
Picon

Re: Data.Map, unionWithMaybe

Dear Sebastiaan,

> -----Original message-----
> From: Sebastiaan Joosten <sjcjoosten <at> gmail.com>
> Sent: 29 Jul 2013, 13:39
>
> Dear libraries,
> 
> for Data.Map, I needed a "unionWithMaybe" function for my sparse system of linear equations
(unionWithMaybe :: Ord k => (a -> a -> Maybe a) -> Map k a -> Map k a -> Map k a). My usage:
> (.+.) = Map.unionWithMaybe (\a b->case a+b of {0->Nothing;s->Just s})
> (I do not think Map.unionWithMaybe can be expressed in terms of other functions without loosing
performance, so I recon it would be a nice addition to the library.)
> 
> I built this function myself by modifying the Data.Map implementation, but it would be nice to see it in the
official version of Data.Map.
> Here is the file including the modifications I made to it. Feel free to use it, I will agree to whatever
license you need to make it public.

The containers version 5.0 (bundled with GHC 7.6) added function
mergeWithKey, which is intended for creating various combining
functions, see http://www.haskell.org/ghc/docs/latest/html/libraries/containers-0.5.0.0/Data-Map-Lazy.html#g:12

You can define unionWithMaybe as
  unionWithMaybe f m1 m2 = mergeWithKey (const f) id id m1 m2

We are most likely not going to add unionWithMaybe to the API of
containers, because you can define it efficiently using mergeWithKey.

Cheers,
(Continue reading)

Sebastiaan Joosten | 29 Jul 16:27 2013
Picon

Re: Data.Map, unionWithMaybe

Great, works like a charm! I wasn't aware of this new function (I just upgraded Haskell to get it). I love how
it works nearly identical for Data.Map and Data.IntMap. Thanks all!

On Jul 29, 2013, at 16:10 , Milan Straka <fox <at> ucw.cz> wrote:

> Dear Sebastiaan,
> 
>> -----Original message-----
>> From: Sebastiaan Joosten <sjcjoosten <at> gmail.com>
>> Sent: 29 Jul 2013, 13:39
>> 
>> Dear libraries,
>> 
>> for Data.Map, I needed a "unionWithMaybe" function for my sparse system of linear equations
(unionWithMaybe :: Ord k => (a -> a -> Maybe a) -> Map k a -> Map k a -> Map k a). My usage:
>> (.+.) = Map.unionWithMaybe (\a b->case a+b of {0->Nothing;s->Just s})
>> (I do not think Map.unionWithMaybe can be expressed in terms of other functions without loosing
performance, so I recon it would be a nice addition to the library.)
>> 
>> I built this function myself by modifying the Data.Map implementation, but it would be nice to see it in
the official version of Data.Map.
>> Here is the file including the modifications I made to it. Feel free to use it, I will agree to whatever
license you need to make it public.
> 
> The containers version 5.0 (bundled with GHC 7.6) added function
> mergeWithKey, which is intended for creating various combining
> functions, see http://www.haskell.org/ghc/docs/latest/html/libraries/containers-0.5.0.0/Data-Map-Lazy.html#g:12
> 
> You can define unionWithMaybe as
>  unionWithMaybe f m1 m2 = mergeWithKey (const f) id id m1 m2
(Continue reading)


Gmane