Proposal for containers: Add 'lookup' function to Data.Set

Nicolas Godbout nicolas.godbout at gmail.com
Mon Jun 27 21:45:29 UTC 2016


WHAT

It is proposed to add a ‘lookup' function on 'Set' in the "containers" package. Feedback during the next two weeks is welcome.

The function

> lookup :: Ord a => a -> Set a -> Maybe a

is almost indentical to the 'member' function but, in addition, returns the value
stored in the set.

WHY

The point of this proposal is to facilitate program-wide data sharing. The 'lookup'
function gives access to a pointer to an object already stored in a Set and equal
to a given argument. The 'lookup' function is a natural extension to the current
'lookupLT', 'lookupGT', 'lookupLE' and 'lookupGE' functions, with obvious semantics.

Example use case: In a parser, the memory footprint can be reduced by collapsing
all equal strings to a single instance of each string. To achieve this, one needs
a way to get a previously seen string (internally, a pointer) equal to a newly
parsed string. Amazingly, this is very difficult with the current "containers" library interface.
One current option is to use a Map instead, e.g., 'Map String String'
which stores twice as many pointers as necessary.

HOW

The git pull request at
https://github.com/haskell/containers/pull/291
contains the straight-forward implementation of the 'lookup’ function on 'Set', with test cases,
as a patch against the current containers master branch.


Salutations,
Nicolas.



More information about the Libraries mailing list