Skip to content

Commit 96d480c

Browse files
committed
Address sjacobi's review comments.
1 parent 7b6a824 commit 96d480c

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

Data/HashSet.hs

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,71 @@ A 'HashSet' makes no guarantees as to the order of its elements.
2626
the set you passed in, they create a new set. In order to keep the changes
2727
you need to assign it to a new variable.
2828
29-
== Example
29+
== Examples
3030
31-
@
32-
import qualified Data.HashSet as HashSet
31+
All the examples below assume @HashSet@ is imported qualified, and uses the following @dataStructures@ set.
3332
34-
-- Create an empty HashSet.
35-
let s = HashSet.'HashSet.empty'
33+
>>> import qualified Data.HashSet as HashSet
34+
>>> let dataStructures = HashSet.fromList ["Set", "Map", "Graph", "Sequence"]
3635
37-
-- Create a HashSet from a list of elements.
38-
let s1 = HashSet.'HashSet.fromList' ["a", "b"]
36+
=== Basic Operations
3937
40-
-- Remove an entry from the HashSet @s1@, storing the new set in @s2@.
41-
let s2 = HashSet.'HashSet.delete' "a" s1
38+
Check membership in a set:
4239
43-
-- Print the @HashSet@ @s1@, notice it still contains the deleted @"a"@.
44-
print s1
45-
> HashSet.'HashSet.fromList' ["a","b"]
40+
>>> -- Check if "Map" and "Trie" are in the set of data structures.
41+
>>> HashSet.member "Map" dataStructures
42+
True
43+
>>> HashSet.member "Trie" dataStructures
44+
False
4645
47-
-- Print the modified @HashSet@, notice that it does not contain the deleted @"a"@.
48-
print s2
49-
> HashSet.'HashSet.fromList' ["b"]
50-
@
46+
Add a new entry to the set:
47+
48+
>>> let moreDataStructures = HashSet.insert "Trie" dataStructures
49+
>>> HashSet.member \"Trie\" moreDataStructures
50+
> True
51+
52+
Remove the @\"Graph\"@ entry from the set of data structures.
53+
54+
>>> let fewerDataStructures = HashSet.delete "Graph" dataStructures
55+
>>> HashSet.toList fewerDataStructures
56+
["Map","Set","Sequence"]
57+
58+
59+
Create a new set and combine it with our original set.
60+
61+
>>> let unorderedDataStructures = HashSet.fromList ["HashSet", "HashMap"]
62+
>>> HashSet.union dataStructures unorderedDataStructures
63+
fromList ["Map","HashSet","Graph","HashMap","Set","Sequence"]
64+
65+
=== Using custom data with HashSet
66+
67+
To create a @HashSet@ of your custom type, you must derive @Eq@ and
68+
@Hashable@. The @Hashable@ typeclass is defined in the
69+
<https://hackage.haskell.org/packages/hashable hashable> package and the
70+
recommended way to define the instance is using generics, for which you'll need
71+
the @DeriveGeneric@ GHC extension.
72+
73+
We'll start by setting up our custom data type:
74+
75+
>>> :set -XDeriveGeneric
76+
>>> -- or {-# DeriveGeneric #-} if writing a Haskell source file.
77+
>>> import GHC.Generics (Generic)
78+
>>> import Data.Hashable
79+
>>> data Person = Person { name :: String, likesDogs :: Bool } deriving (Show, Eq, Generic)
80+
>>> instance Hashable Person
81+
82+
And now we'll use it!
83+
84+
>>> let people = HashSet.fromList [Person "Lana" True, Person "Joe" False, Person "Simon" True]
85+
>>> HashSet.filter likesDogs people
86+
fromList [Person {name = "Simon", likesDogs = True},Person {name = "Lana", likesDogs = True}]
5187
52-
__IMPORTANT:__ @HashSet@ relies on the @element@ type having instances of the
53-
@Eq@ and @Hashable@ typeclasses for its internal representation. These are
54-
already defined for builtin types, and if you are using your own data type you
55-
can use the <https://en.wikibooks.org/wiki/Haskell/Classes_and_types#Deriving deriving>
56-
mechanism.
5788
5889
== Performance
5990
6091
The implementation is based on /hash array mapped trie/. A
61-
'HashSet' is often faster than other tree-based set types,
62-
especially when value comparison is expensive, as in the case of
92+
'HashSet' is often faster than other 'Ord'-based set types,
93+
especially when value comparisons are expensive, as in the case of
6394
strings.
6495
6596
Many operations have a average-case complexity of /O(log n)/. The

0 commit comments

Comments
 (0)