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 l removes duplicate elements from a list. In particular, it keeps only the first occurrence of each element.

dedup is stable so the elements in the output are ordered by their first occurrence in the input. If you do not need stability, consider using dedupSort which is more efficient.

>>> dedup [3, 1, 1, 3]
[3, 1]
dedupOn

: Ord k => (v -> k) -> [v] -> [v]

A version of dedup where deduplication is done after applying the given function. Example use: dedupOn (.employeeNo) employees.

dedupOn is stable so the elements in the output are ordered by their first occurrence in the input. If you do not need stability, consider using dedupOnSort which is more efficient.

>>> dedupOn fst [(3, "a"), (1, "b"), (1, "c"), (3, "d")]
[(3, "a"), (1, "b")]
dedupSort

: Ord a => [a] -> [a]

dedupSort is a more efficient variant of dedup that 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]

dedupOnSort is a more efficient variant of dedupOn that 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 f is a version of sort that allows sorting on the result of the given function.

sortOn is 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

: Ord a => [a] -> Bool

Returns True if and only if there are no duplicate elements in the given list.

>>> unique [1, 2, 3]
True
uniqueOn

: Ord k => (a -> k) -> [a] -> Bool

Returns True if and only if there are no duplicate elements in the given list after applyng function.

>>> uniqueOn fst [(1, 2), (2, 42), (1, 3)]
False