Skip to content

Commit c977e9f

Browse files
committed
DO NOT SUBMIT: Some partial conversion to Tutorial module.
1 parent 6261871 commit c977e9f

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

Tutorial.hs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
2+
3+
{-|
4+
The @unordered-containers@ package provides implementations of various
5+
hash-based immutable data structures.
6+
7+
Some of the data structures provided by this package have a very large API
8+
surface (for better or worse). The docs here focus on the most common functions
9+
which should be more than enough to get you started. Once you know the basics,
10+
or if you're looking for a specific function, you can head over to the
11+
full API documentation!
12+
-}
13+
14+
15+
module Tutorial (
16+
-- * Provided Data Structures
17+
-- $provideddatastructures
18+
19+
-- * Related Packages
20+
-- $relatedpackages
21+
22+
-- * Looking for more resources?
23+
-- $moreresources
24+
25+
-- * Installing and using the @unordered-containers@ packages
26+
-- $installing
27+
28+
-- * HashSet and HashMap tutorial
29+
-- $tutorials
30+
31+
) where
32+
33+
{- $provideddatastructures
34+
* 'Data.HashSet' - unordered, non-duplicated elements
35+
* 'Data.HashMap' - unordered map from keys to values (aka. dictionaries)
36+
-}
37+
38+
{- $relatedpackages
39+
* <https://hackage.haskell.org/packages/containers containers> - ordered containers using trees instead of hashing.
40+
* <https://hackage.haskell.org/packages/hashable containers> - types that can be converted to a hash value.
41+
-}
42+
43+
{- $moreresources
44+
If you've worked your way through the documentation here and you're looking for
45+
more examples or tutorials you should check out:
46+
47+
* <https://haskell-lang.org/library/containers haskell-lang.org's containers tutorial>, its focused on the ordered
48+
<https://hackage.haskell.org/packages/containers containers> library but provides some useful examples.
49+
* <http://learnyouahaskell.com/modules Learn You a Haskell "Modules" chapter>
50+
-}
51+
52+
{- $installing
53+
__Version Requirements__
54+
55+
All of the examples here should work for all recent versions of the package.
56+
57+
__Importing modules__
58+
59+
All of the modules in @unordered-containers@@ should be imported @qualified@
60+
since they use names that conflict with the standard Prelude.
61+
62+
@
63+
import qualified Data.HashSet as HashSet
64+
import qualified Data.HashMap.Strict as HashMap
65+
@
66+
67+
__In GHCi__
68+
69+
Start the GHCi
70+
<https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop REPL> with
71+
@ghci@, @cabal repl@, or @stack ghci@. Once the REPL is loaded, import the
72+
modules you want using the @import@ statements above and you're good to go!
73+
-}
74+
75+
76+
{- $tutorials
77+
78+
See "Tutorial.HashSet" and "Tutorial.HashMap".
79+
80+
-}

Tutorial/HashSet.hs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
2+
3+
{-|
4+
Sets allow you to store *unique* elements, providing efficient insertion,
5+
lookups, and deletions. If you are storing sets of @Int@ s consider using
6+
'Data.IntSet' from the <https://hackage.haskell.org/packages/containers containers> package. You can find the
7+
introductory documentation for @containers@ at
8+
<https://haskell-containers.readthedocs.io>.
9+
10+
@
11+
data HashSet element = ...
12+
@
13+
14+
15+
All of these implementations are *immutable* which means that any update
16+
functions do not modify the set that you passed in, they creates a new set. In
17+
order to keep the changes you need to assign it to a new variable. For example:
18+
19+
@
20+
let s1 = HashSet.fromList ["a", "b"]
21+
let s2 = HashSet.delete "a" s1
22+
print s1
23+
> fromList ["a","b"]
24+
print s2
25+
> fromList ["b"]
26+
@
27+
28+
__IMPORTANT:__ @HashSet@ relies on the @element@ type having instances of the @Eq@ and
29+
@Hashable@ typeclasses for its internal representation. These are already
30+
defined for builtin types, and if you are using your own data type you can
31+
use the
32+
<https://en.wikibooks.org/wiki/Haskell/Classes_and_types#Deriving deriving>
33+
mechanism.
34+
35+
36+
-}
37+
38+
module Tutorial.HashSet (
39+
-- * Short Example
40+
-- $shortexample
41+
42+
) where
43+
44+
45+
{- $shortexample
46+
47+
The following GHCi session shows some of the basic set functionality:
48+
49+
@
50+
import qualified Data.HashSet as HashSet
51+
52+
let dataStructures = HashSet.fromList ["HashSet", "HashMap", "Graph"]
53+
54+
-- Check if "HashMap" and "Trie" are in the set of data structures.
55+
HashSet.member "HashMap" dataStructures
56+
> True
57+
58+
HashSet.member "Trie" dataStructures
59+
> False
60+
61+
62+
-- Add "Trie" to our original set of data structures.
63+
let moreDataStructures = HashSet.insert "Trie" dataStructures
64+
65+
HashSet.member "Trie" moreDataStructures
66+
> True
67+
68+
69+
-- Remove "Graph" from our original set of data structures.
70+
let fewerDataStructures = HashSet.delete "Graph" dataStructures
71+
72+
HashSet.toList fewerDataStructures
73+
> ["HashSet", "HashMap"]
74+
75+
76+
-- Create a new set and combine it with our original set.
77+
let orderedDataStructures = HashSet.fromList ["Set", "Map"]
78+
79+
HashSet.union dataStructures orderedDataStructures
80+
> fromList ["Map", "HashSet", "Graph", "HashMap", "Set"]
81+
@
82+
83+
84+
__TIP__: You can use the `OverloadedLists
85+
<https://ghc.haskell.org/trac/ghc/wiki/OverloadedLists>`_ extension so
86+
you don't need to write ``fromList [1, 2, 3]`` everywhere. Instead you
87+
can just write ``[1, 2, 3]`` and if the function is expecting a set it
88+
will be converted automatically! The code here will continue to use
89+
``fromList`` for clarity though.
90+
91+
92+
-}

unordered-containers.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ library
3131
Data.HashMap.Lazy
3232
Data.HashMap.Strict
3333
Data.HashSet
34+
Tutorial
35+
Tutorial.HashSet
3436
other-modules:
3537
Data.HashMap.Array
3638
Data.HashMap.Base

0 commit comments

Comments
 (0)