Proposal: Add splitL and splitR to container’s Map APIs

Joachim Breitner mail at joachim-breitner.de
Tue Jan 31 19:44:48 UTC 2017


Hi,

I regularly find myself in the need of splitting a map into two maps.
For that we currently have these two functions:

split       :: Ord k => k -> Map k a -> (Map k a, Map k a)
splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a) 

Usually, split is useless to me because information (the element right
on the border) gets lost. splitLookup preserves the information, but
often I want the element at the border to simply be in one of the two
output maps. So I would like to see

splitL :: Ord k => k -> Map k a -> (Map k a, Map k a)
splitR :: Ord k => k -> Map k a -> (Map k a, Map k a)

with these properties

uncurry union (splitL k m) == m
uncurry union (splitR k m) == m
all (<= k) (keys (fst (splitL k m))
all (> k)  (keys (snd (splitL k m))
all (< k)  (keys (fst (splitR k m))
all (>= k) (keys (snd (splitR k m))

These new functions should be added to Data.Map, Data.IntMap (in both
variatans each) and, for consistency, Data.Set.

Alternative names (in correspondence with Data.Set.lookup{LT,LE,GT,TE})
would be splitLE (instead of splitL) and splitLT (instead of splitR).

Corresponding issue: https://github.com/haskell/containers/issues/387
Discussion period:   2 weeks (until Feb 14).

Greetings,
Joachim

-- 
Joachim “nomeata” Breitner
  mail at joachim-breitner.dehttps://www.joachim-breitner.de/
  XMPP: nomeata at joachim-breitner.de • OpenPGP-Key: 0xF0FBF51F
  Debian Developer: nomeata at debian.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20170131/98ad8494/attachment.sig>


More information about the Libraries mailing list