Module DA.List.BuiltinOrder¶
Note: This is only supported in Daml-LF 1.11 or later.
This module provides variants of other standard library
functions that are based on the builtin Daml-LF ordering rather
than user-defined ordering. This is the same order also used
by DA.Map.
These functions are usually much more efficient than their
Ord-based counterparts.
Note that the functions in this module still require Ord
constraints. This is purely to enforce that you don’t
pass in values that cannot be compared, e.g., functions. The
implementation of those instances is not used.
Functions¶
- dedup
: Ord a => [a] -> [a]
dedup lremoves duplicate elements from a list. In particular, it keeps only the first occurrence of each element.dedupis stable so the elements in the output are ordered by their first occurrence in the input. If you do not need stability, consider usingdedupSortwhich is more efficient.>>> dedup [3, 1, 1, 3] [3, 1]
- dedupOn
: Ord k => (v -> k) -> [v] -> [v]
A version of
dedupwhere deduplication is done after applying the given function. Example use:dedupOn (.employeeNo) employees.dedupOnis stable so the elements in the output are ordered by their first occurrence in the input. If you do not need stability, consider usingdedupOnSortwhich is more efficient.>>> dedupOn fst [(3, "a"), (1, "b"), (1, "c"), (3, "d")] [(3, "a"), (1, "b")]
- dedupSort
: Ord a => [a] -> [a]
dedupSortis a more efficient variant ofdedupthat does not preserve the order of the input elements. Instead the output will be sorted acoording to the builtin Daml-LF ordering.>>> dedupSort [3, 1, 1, 3] [1, 3]
- dedupOnSort
: Ord k => (v -> k) -> [v] -> [v]
dedupOnSortis a more efficient variant ofdedupOnthat does not preserve the order of the input elements. Instead the output will be sorted on the values returned by the function.For duplicates, the first element in the list will be included in the output.
>>> dedupOnSort fst [(3, "a"), (1, "b"), (1, "c"), (3, "d")] [(1, "b"), (3, "a")]
- sort
: Ord a => [a] -> [a]
Sort the list according to the Daml-LF ordering.
Values that are identical according to the builtin Daml-LF ordering are indistinguishable so stability is not relevant here.
>>> sort [3,1,2] [1,2,3]
- sortOn
: Ord b => (a -> b) -> [a] -> [a]
sortOn fis a version of sort that allows sorting on the result of the given function.sortOnis stable so elements that map to the same sort key will be ordered by their position in the input.>>> sortOn fst [(3, "a"), (1, "b"), (3, "c"), (2, "d")] [(1, "b"), (2, "d"), (3, "a"), (3, "c")]
- unique
-
Returns True if and only if there are no duplicate elements in the given list.
>>> unique [1, 2, 3] True