Proposed additions to Data.Map: lookupFloor and lookupCeiling

Sean Leather leather at
Mon Mar 1 05:34:05 EST 2010

On Mon, Mar 1, 2010 at 09:40, Leon Smith wrote:

> On Mon, Mar 1, 2010 at 2:39 AM, Sean Leather wrote:
> > It seems more intuitive to reverse the naming. The key parameter to
> > lookupFloor is actually the ceiling, and the key returned is the greatest
> > key less than or equal to the ceiling. Since there is no floor involved
> > here, I would call the function lookupCeiling.
> The greatest number less than something would be the floor.  :-)

So, what I didn't say (but was thinking) was that I would find these
renamings more intuitive:

lookupFloor --> lookupWithCeiling
lookupCeiling --> lookupWithFloor

These tell me that the key is the ceiling or floor value.

For example:
> ghci>  let map = Map.fromList [ (x, ()) | x <- [0,10..1000] ]
> ghci>  lookupFloor 75 map
>   Just (70, ())
> ghci>  floor 7.5
>   7
> ghci>  lookupCeiling 115 map
>   Just (120,())
> ghci>  ceiling 11.5
>   12

I understand where you're coming from. You're mapping the set of keys to the
set of integers and applying the (e.g.) floor function to the key within
that set. Alternative (though verbose) names for your functions might be:

lookupFloor --> applyFloorAndLookup
lookupCeiling --> applyCeilingAndLookup

In contrast, I looked at the parameter as being the lower or upper bound.
So, perhaps an even better naming (from my point of view) would be:

lookupFloor --> lookupWithUpperBound or lookupUpperBound or lookupUB or ...
lookupCeiling --> lookupWithLowerBound or lookupLowerBound or lookupLB or

Something like this might also remove any confusion about floor and ceiling.

But honestly I almost invariably get the definitions of floor and
> ceiling wrong unless I stop to think for a few seconds.   I've trained
> myself to do that.  ;-)

With your current naming scheme, I think I would constantly choose the
opposite function. But maybe that's just me.

Maybe a few examples in the haddocks would
> be in order.   That's easier to comprehend,  if you know approximately
> what it should do.

Whatever is the end result, I think you should definitely have some examples
in the documentation.

Also, it occurs to me that maybe a combined function :: (Ord k) => k
> -> Map k v -> (Maybe k v, Maybe k v)  would be useful in addition to
> or in place of lookupFloor and lookupCeiling.  But I'm not sure.

I don't know either.

-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Libraries mailing list