This Commented CUE
demonstrates how to use the built-in function
list.FlattenN
to flatten a list by expanding its list elements by a specified depth.
file.cue
package example
import "list"
// src is a list (of lists (of lists ...)), defined below.
src: [...]
// one transforms src by expanding its first-level list elements.
one: list.FlattenN(src, 1)
// two transforms src by expanding its first- and second-level list elements.
two: list.FlattenN(src, 2)
// all transforms src by expanding all its list elements, recursively, no
// matter their depth.
all: list.FlattenN(src, -1)
src: [
1, 2, 3,
["a", "b", "c"],
[
[4, 5, 6],
[
["d", "e", "f"],
[[7, 8, 9]],
],
],
[[[[["g", "h", "i"]]]]],
]
TERMINAL
$ cue eval
src: [1, 2, 3, ["a", "b", "c"], [[4, 5, 6], [["d", "e", "f"], [[7, 8, 9]]]], [[[[["g", "h", "i"]]]]]]
one: [1, 2, 3, "a", "b", "c", [4, 5, 6], [["d", "e", "f"], [[7, 8, 9]]], [[[["g", "h", "i"]]]]]
two: [1, 2, 3, "a", "b", "c", 4, 5, 6, ["d", "e", "f"], [[7, 8, 9]], [[["g", "h", "i"]]]]
all: [1, 2, 3, "a", "b", "c", 4, 5, 6, "d", "e", "f", 7, 8, 9, "g", "h", "i"]
Related content
- The
list
built-in package