[Haskell-cafe] Data.Map: Enumerating ordered subset of keys

Jared Updike jupdike at gmail.com
Mon Feb 9 11:33:49 EST 2009

On 2/8/09, Evan Laforge <qdunkan at gmail.com> wrote:
> I have a little util library with various map functions.  'within' is
>  almost what you want, except it's half-open.  You can make an
>  inclusive one by pulling the lowest element off the above map.
>  I'm also curious if there's a better way to do this...
>  -- | Like Map.split, except include a matched key in the above map.
>  split_map :: (Ord k) => k -> Map.Map k a -> (Map.Map k a, Map.Map k a)
>  split_map k fm = (pre, post')
>     where
>     (pre, at, post) = Map.splitLookup k fm
>     post' = maybe post (\v -> Map.insert k v post) at
>  -- | Split the map into the maps below, within, and above the given range.
>  -- @low@ to @high@ is half-open, as usual.
>  split3_map :: (Ord k) => k -> k -> Map.Map k a
>     -> (Map.Map k a, Map.Map k a, Map.Map k a)
>  split3_map low high fm = (below, within, way_above)
>     where
>     (below, above) = split_map low fm
>     (within, way_above) = split_map high above
>  within low high fm = let (_, m, _) = split3_map low high fm in m

This looks right to me (correct time complexity). It should do what I
need. I will test it and see what I discover.


More information about the Haskell-Cafe mailing list