@@ -63,27 +63,27 @@ Example usage:
6363
6464import SeqDict exposing (SeqDict )
6565
66- import Set exposing (Set )
66+ import SeqSet exposing (SeqSet )
6767
6868
6969{- | The underlying data structure. Think about it as
7070
7171 type alias BiSeqDict a b =
7272 { forward : SeqDict a b -- just a normal Dict!
73- , reverse : SeqDict b (Set a) -- the reverse mappings!
73+ , reverse : SeqDict b (SeqSet a) -- the reverse mappings!
7474 }
7575
7676-}
77- type BiSeqDict comparable1 comparable2
77+ type BiSeqDict k v
7878 = BiSeqDict
79- { forward : SeqDict comparable1 comparable2
80- , reverse : SeqDict comparable2 ( Set comparable1 )
79+ { forward : SeqDict k v
80+ , reverse : SeqDict v ( SeqSet k )
8181 }
8282
8383
8484{- | Create an empty dictionary.
8585-}
86- empty : BiSeqDict comparable1 comparable2
86+ empty : BiSeqDict k v
8787empty =
8888 BiSeqDict
8989 { forward = SeqDict . empty
@@ -93,18 +93,18 @@ empty =
9393
9494{- | Create a dictionary with one key-value pair.
9595-}
96- singleton : comparable1 -> comparable2 -> BiSeqDict comparable1 comparable2
96+ singleton : k -> v -> BiSeqDict k v
9797singleton from to =
9898 BiSeqDict
9999 { forward = SeqDict . singleton from to
100- , reverse = SeqDict . singleton to ( Set . singleton from)
100+ , reverse = SeqDict . singleton to ( SeqSet . singleton from)
101101 }
102102
103103
104104{- | Insert a key-value pair into a dictionary. Replaces value when there is
105105a collision.
106106-}
107- insert : comparable1 -> comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
107+ insert : k -> v -> BiSeqDict k v -> BiSeqDict k v
108108insert from to ( BiSeqDict d) =
109109 BiSeqDict
110110 { d
@@ -122,7 +122,7 @@ insert from to (BiSeqDict d) =
122122 Just oldTo_ ->
123123 d. reverse
124124 |> SeqDict . update oldTo_
125- ( Maybe . map ( Set . remove from)
125+ ( Maybe . map ( SeqSet . remove from)
126126 >> Maybe . andThen normalizeSet
127127 )
128128 in
@@ -133,7 +133,7 @@ insert from to (BiSeqDict d) =
133133
134134{- | Update the value of a dictionary for a specific key with a given function.
135135-}
136- update : comparable1 -> (Maybe comparable2 -> Maybe comparable2 ) -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
136+ update : k -> (Maybe v -> Maybe v ) -> BiSeqDict k v -> BiSeqDict k v
137137update from fn ( BiSeqDict d) =
138138 SeqDict . update from fn d. forward
139139 |> fromDict
@@ -142,7 +142,7 @@ update from fn (BiSeqDict d) =
142142{- | In our model, (Just Set.empty) has the same meaning as Nothing.
143143Make it be Nothing!
144144-}
145- normalizeSet : Set comparable -> Maybe (Set comparable )
145+ normalizeSet : Set comparable -> Maybe (SeqSet comparable )
146146normalizeSet set =
147147 if Set . isEmpty set then
148148 Nothing
@@ -154,7 +154,7 @@ normalizeSet set =
154154{- | Remove a key-value pair from a dictionary. If the key is not found,
155155no changes are made.
156156-}
157- remove : comparable1 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
157+ remove : k -> BiSeqDict k v -> BiSeqDict k v
158158remove from ( BiSeqDict d) =
159159 BiSeqDict
160160 { d
@@ -168,14 +168,14 @@ remove from (BiSeqDict d) =
168168 isEmpty empty == True
169169
170170-}
171- isEmpty : BiSeqDict comparable1 comparable2 -> Bool
171+ isEmpty : BiSeqDict k v -> Bool
172172isEmpty ( BiSeqDict d) =
173173 SeqDict . isEmpty d. forward
174174
175175
176176{- | Determine if a key is in a dictionary.
177177-}
178- member : comparable1 -> BiSeqDict comparable1 comparable2 -> Bool
178+ member : k -> BiSeqDict k v -> Bool
179179member from ( BiSeqDict d) =
180180 SeqDict . member from d. forward
181181
@@ -191,23 +191,23 @@ dictionary.
191191 get "Spike" animals == Nothing
192192
193193-}
194- get : comparable1 -> BiSeqDict comparable1 comparable2 -> Maybe comparable2
194+ get : k -> BiSeqDict k v -> Maybe v
195195get from ( BiSeqDict d) =
196196 SeqDict . get from d. forward
197197
198198
199199{- | Get the keys associated with a value. If the value is not found,
200200return an empty set.
201201-}
202- getReverse : comparable2 -> BiSeqDict comparable1 comparable2 -> Set comparable1
202+ getReverse : v -> BiSeqDict k v -> Set k
203203getReverse to ( BiSeqDict d) =
204204 SeqDict . get to d. reverse
205205 |> Maybe . withDefault Set . empty
206206
207207
208208{- | Determine the number of key-value pairs in the dictionary.
209209-}
210- size : BiSeqDict comparable1 comparable2 -> Int
210+ size : BiSeqDict k v -> Int
211211size ( BiSeqDict d) =
212212 SeqDict . size d. forward
213213
@@ -217,7 +217,7 @@ size (BiSeqDict d) =
217217 keys (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ]) == [ 0, 1 ]
218218
219219-}
220- keys : BiSeqDict comparable1 comparable2 -> List comparable1
220+ keys : BiSeqDict k v -> List k
221221keys ( BiSeqDict d) =
222222 SeqDict . keys d. forward
223223
@@ -227,50 +227,50 @@ keys (BiSeqDict d) =
227227 values (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ]) == [ "Alice", "Bob" ]
228228
229229-}
230- values : BiSeqDict comparable1 comparable2 -> List comparable2
230+ values : BiSeqDict k v -> List v
231231values ( BiSeqDict d) =
232232 SeqDict . values d. forward
233233
234234
235235{- | Get a list of unique values in the dictionary.
236236-}
237- uniqueValues : BiSeqDict comparable1 comparable2 -> List comparable2
237+ uniqueValues : BiSeqDict k v -> List v
238238uniqueValues ( BiSeqDict d) =
239239 SeqDict . keys d. reverse
240240
241241
242242{- | Get a count of unique values in the dictionary.
243243-}
244- uniqueValuesCount : BiSeqDict comparable1 comparable2 -> Int
244+ uniqueValuesCount : BiSeqDict k v -> Int
245245uniqueValuesCount ( BiSeqDict d) =
246246 SeqDict . size d. reverse
247247
248248
249249{- | Convert a dictionary into an association list of key-value pairs, sorted by keys.
250250-}
251- toList : BiSeqDict comparable1 comparable2 -> List ( comparable1 , comparable2 )
251+ toList : BiSeqDict k v -> List ( k , v )
252252toList ( BiSeqDict d) =
253253 SeqDict . toList d. forward
254254
255255
256256{- | Convert a dictionary into a reverse association list of value-keys pairs.
257257-}
258- toReverseList : BiSeqDict comparable1 comparable2 -> List ( comparable2 , Set comparable1 )
258+ toReverseList : BiSeqDict k v -> List ( v , Set k )
259259toReverseList ( BiSeqDict d) =
260260 SeqDict . toList d. reverse
261261
262262
263263{- | Convert an association list into a dictionary.
264264-}
265- fromList : List ( comparable1 , comparable2 ) -> BiSeqDict comparable1 comparable2
265+ fromList : List ( k , v ) -> BiSeqDict k v
266266fromList list =
267267 SeqDict . fromList list
268268 |> fromDict
269269
270270
271271{- | Apply a function to all values in a dictionary.
272272-}
273- map : (comparable1 -> comparable21 -> comparable22 ) -> BiSeqDict comparable1 comparable21 -> BiSeqDict comparable1 comparable22
273+ map : (k -> v1 -> v2 ) -> BiSeqDict k v1 -> BiSeqDict k v2
274274map fn ( BiSeqDict d) =
275275 -- TODO diff instead of throwing away and creating from scratch?
276276 SeqDict . map fn d. forward
@@ -279,14 +279,14 @@ map fn (BiSeqDict d) =
279279
280280{- | Convert BiSeqDict into a SeqDict. (Throw away the reverse mapping.)
281281-}
282- toDict : BiSeqDict comparable1 comparable2 -> SeqDict comparable1 comparable2
282+ toDict : BiSeqDict k v -> SeqDict k v
283283toDict ( BiSeqDict d) =
284284 d. forward
285285
286286
287287{- | Convert Dict into a BiSeqDict. (Compute the reverse mapping.)
288288-}
289- fromDict : SeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
289+ fromDict : SeqDict k v -> BiSeqDict k v
290290fromDict forward =
291291 BiSeqDict
292292 { forward = forward
@@ -322,7 +322,7 @@ fromDict forward =
322322 -- getAges users == [33,19,28]
323323
324324-}
325- foldl : (comparable1 -> comparable2 -> acc -> acc ) -> acc -> BiSeqDict comparable1 comparable2 -> acc
325+ foldl : (k -> v -> acc -> acc ) -> acc -> BiSeqDict k v -> acc
326326foldl fn zero ( BiSeqDict d) =
327327 SeqDict . foldl fn zero d. forward
328328
@@ -339,14 +339,14 @@ foldl fn zero (BiSeqDict d) =
339339 -- getAges users == [28,19,33]
340340
341341-}
342- foldr : (comparable1 -> comparable2 -> acc -> acc ) -> acc -> BiSeqDict comparable1 comparable2 -> acc
342+ foldr : (k -> v -> acc -> acc ) -> acc -> BiSeqDict k v -> acc
343343foldr fn zero ( BiSeqDict d) =
344344 SeqDict . foldr fn zero d. forward
345345
346346
347347{- | Keep only the key-value pairs that pass the given test.
348348-}
349- filter : (comparable1 -> comparable2 -> Bool ) -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
349+ filter : (k -> v -> Bool ) -> BiSeqDict k v -> BiSeqDict k v
350350filter fn ( BiSeqDict d) =
351351 -- TODO diff instead of throwing away and creating from scratch?
352352 SeqDict . filter fn d. forward
@@ -357,7 +357,7 @@ filter fn (BiSeqDict d) =
357357contains all key-value pairs which passed the test, and the second contains
358358the pairs that did not.
359359-}
360- partition : (comparable1 -> comparable2 -> Bool ) -> BiSeqDict comparable1 comparable2 -> ( BiSeqDict comparable1 comparable2 , BiSeqDict comparable1 comparable2 )
360+ partition : (k -> v -> Bool ) -> BiSeqDict k v -> ( BiSeqDict k v , BiSeqDict k v )
361361partition fn ( BiSeqDict d) =
362362 -- TODO diff instead of throwing away and creating from scratch?
363363 let
@@ -372,7 +372,7 @@ partition fn (BiSeqDict d) =
372372{- | Combine two dictionaries. If there is a collision, preference is given
373373to the first dictionary.
374374-}
375- union : BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
375+ union : BiSeqDict k v -> BiSeqDict k v -> BiSeqDict k v
376376union ( BiSeqDict left) ( BiSeqDict right) =
377377 -- TODO diff instead of throwing away and creating from scratch?
378378 SeqDict . union left. forward right. forward
@@ -382,7 +382,7 @@ union (BiSeqDict left) (BiSeqDict right) =
382382{- | Keep a key-value pair when its key appears in the second dictionary.
383383Preference is given to values in the first dictionary.
384384-}
385- intersect : BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
385+ intersect : BiSeqDict k v -> BiSeqDict k v -> BiSeqDict k v
386386intersect ( BiSeqDict left) ( BiSeqDict right) =
387387 -- TODO diff instead of throwing away and creating from scratch?
388388 SeqDict . intersect left. forward right. forward
@@ -391,7 +391,7 @@ intersect (BiSeqDict left) (BiSeqDict right) =
391391
392392{- | Keep a key-value pair when its key does not appear in the second dictionary.
393393-}
394- diff : BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
394+ diff : BiSeqDict k v -> BiSeqDict k v -> BiSeqDict k v
395395diff ( BiSeqDict left) ( BiSeqDict right) =
396396 -- TODO diff instead of throwing away and creating from scratch?
397397 SeqDict . diff left. forward right. forward
@@ -410,11 +410,11 @@ you want.
410410
411411-}
412412merge :
413- ( comparable1 -> comparable21 -> acc -> acc)
414- -> ( comparable1 -> comparable21 -> comparable22 -> acc -> acc)
415- -> ( comparable1 -> comparable22 -> acc -> acc)
416- -> BiSeqDict comparable1 comparable21
417- -> BiSeqDict comparable1 comparable22
413+ ( k -> v1 -> acc -> acc)
414+ -> ( k -> v1 -> v2 -> acc -> acc)
415+ -> ( k -> v2 -> acc -> acc)
416+ -> BiSeqDict k v1
417+ -> BiSeqDict k v2
418418 -> acc
419419 -> acc
420420merge fnLeft fnBoth fnRight ( BiSeqDict left) ( BiSeqDict right) zero =
0 commit comments