This Commented CUE demonstrates how to produce a list of unique strings from a list of strings that might include duplicate values.
file.cue
package example
import "list"
stringList: ["d", "a", "b", "a", "c", "b", "b"]
stringSet: {
let map = {for v in stringList {(v): _}}
list.SortStrings([for k, _ in map {k}])
}
TERMINAL
$ cue eval
stringList: ["d", "a", "b", "a", "c", "b", "b"]
stringSet: ["a", "b", "c", "d"]
It is not strictly necessary to sort the elements of the set.
We do this to ensure a deterministic output.
Related content
- Language Tour: Field Comprehensions
- Language Tour: List Comprehensions
- Language Specification: Embedding