rtfeldman/elm-sorter-experiment/2.1.0

NOTE: the latest version is 2.1.2

Rerouted (retagged, verified). The tag now points at a different commit than when this version was published. The Lamdera package server serves a copy recovered from https://github.com/rtfeldman/elm-sorter-experiment.

What we checked: Tag 2.1.0 now points at 76d3ee545ee4, not the published commit. Rebuilt with git archive from commit 70c636c297ef of https://github.com/rtfeldman/elm-sorter-experiment (zip prefix rtfeldman-elm-sorter-experiment-70c636c/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 a6ae57db2a43d6f13840e5b912bf0b70134eeb7b.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/rtfeldman/elm-sorter-experiment/zipball/2.1.0/

Sort.Dict

A dictionary mapping unique keys to values.

Insert, remove, and query operations all take O(log n) time.

This implementation is based on Skinney/elm-dict-exploration, except that it uses Sorter instead of comparable, so it permits keys that are not comparable.

Dictionaries

type alias Dict key value =
Dict key value

A dictionary of keys and values. So a (Dict String User) is a dictionary that lets you look up a String (such as user names) and find the associated User.

Build

empty : Sorter k -> Dict k v

Create an empty dictionary.

singleton : Sorter k -> k -> v -> Dict k v

Create a dictionary with one key-value pair.

insert : k -> v -> Dict k v -> Dict k v

Insert a key-value pair in a dictionary. Replaces value when that key is already present.

insertAll : Dict k v -> Dict k v -> Dict k v

Take all the key-value pairs in the first dictionary and insert them into the second dictionary.

remove : k -> Dict k v -> Dict k v

Remove a key-value pair from a dictionary. If the key is not found, no changes are made.

update : k -> (Maybe v -> Maybe v) -> Dict k v -> Dict k v

Update the value of a dictionary for a specific key with a given function. The given function gets the current value as a parameter and its return value determines if the value is updated or removed. New key-value pairs can be stored too.

-> (k -> a -> result -> result)
-> (k -> a -> b -> result -> result)
-> (k -> b -> result -> result)
-> Dict k a
-> Dict k b
-> result
-> result

The most general way of combining two dictionaries. You provide three accumulators for when a given key appears:

  1. Only in the first dictionary
  2. In both dictionaries
  3. Only in the second dictionary

You then traverse all the keys, building up whatever you want.

The given Sorter will be used to compare the keys.

Query

isEmpty : Dict k v -> Bool

Determine if a dictionary is empty. isEmpty empty == True

size : Dict k v -> Int

Determine the number of key-value pairs in the dictionary.

get : k -> Dict k v -> Maybe v

Get the value associated with a key. If the key is not found, return Nothing. This is useful when you are not sure if a key will be in the dictionary.

animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
get "Tom" animals == Just Cat
get "Jerry" animals == Just Mouse
get "Spike" animals == Nothing
memberOf : Dict k v -> k -> Bool

Return True if the given key is in the given dictionary.

eq : Dict k v -> Dict k v -> Bool

Check if two dictionaries are equal.

Transform

map : (k -> a -> b) -> Dict k a -> Dict k b

Apply a function to all values in a dictionary.

keepIf : (k -> v -> Bool) -> Dict k v -> Dict k v

Keep a key-value pair only if it satisfies a predicate.

dropIf : (k -> v -> Bool) -> Dict k v -> Dict k v

Remove a key-value pair if it satisfies a predicate.

foldl : (k -> v -> b -> b) -> b -> Dict k v -> b

Fold over the key-value pairs in a dictionary, in order from lowest key to highest key.

foldr : (k -> v -> b -> b) -> b -> Dict k v -> b

Fold over the key-value pairs in a dictionary, in order from highest key to lowest key.

partition : (k -> v -> Bool) -> Dict k v -> ( Dict k v, Dict k v )

Partition a dictionary according to a predicate. The first dictionary contains all key-value pairs which satisfy the predicate, and the second contains the rest.

Lists

keys : Dict k v -> List k

Get all of the keys in a dictionary, sorted from lowest to highest. keys (fromList [(0,"Alice"),(1,"Bob")]) == [0,1]

values : Dict k v -> List v

Get all of the values in a dictionary, in the order of their keys. values (fromList [(0,"Alice"),(1,"Bob")]) == ["Alice", "Bob"]

toList : Dict k v -> List ( k, v )

Convert a dictionary into an association list of key-value pairs, sorted by keys.

fromList : Sorter k -> List ( k, v ) -> Dict k v

Convert an association list into a dictionary.