Skip to content

Commit

Permalink
docs/howto: use list.FlattenN
Browse files Browse the repository at this point in the history
This adds a Commented CUE demonstrating how to use list.FlattenN to
expand a list's elements to different depths.

Preview-Path: /docs/howto/use-the-built-in-function-list-flattenn-to-flatten-lists/
Signed-off-by: Jonathan Matthews <[email protected]>
Change-Id: I97415642812fa137cd5c2dd4e6efadd3c5d80a3d
  • Loading branch information
jpluscplusm committed Jan 18, 2024
1 parent 50253be commit 52e8869
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: Using the built-in function "list.FlattenN" to flatten lists
tags:
- commented cue
authors:
- jpluscplusm
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to use the built-in function `list.FlattenN` to flatten a list
by expanding its list elements down to a specified depth.

{{{with code "en" "cc"}}}
#location top bottom

exec cue eval
cmp stdout out
-- file.cue --
package example

import "list"

// lSrc is a list (of lists (of lists ...)), defined below
lSrc: [...]

// eOne's first-level list elements are expanded
eOne: list.FlattenN(lSrc, 1)
// eTwo's first- and second-level list elements are expanded
eTwo: list.FlattenN(lSrc, 2)
// All of eNeg's list elements are expanded, no matter their depth
eNeg: list.FlattenN(lSrc, -1)

lSrc: [
1, 2, 3,
["a", "b", "c"],
[
[4, 5, 6],
[
["d", "e", "f"],
[[7, 8, 9]],
],
],
[[[[["g", "h", "i"]]]]],
]
-- out --
lSrc: [1, 2, 3, ["a", "b", "c"], [[4, 5, 6], [["d", "e", "f"], [[7, 8, 9]]]], [[[[["g", "h", "i"]]]]]]
eOne: [1, 2, 3, "a", "b", "c", [4, 5, 6], [["d", "e", "f"], [[7, 8, 9]]], [[[["g", "h", "i"]]]]]
eTwo: [1, 2, 3, "a", "b", "c", 4, 5, 6, ["d", "e", "f"], [[7, 8, 9]], [[["g", "h", "i"]]]]
eNeg: [1, 2, 3, "a", "b", "c", 4, 5, 6, "d", "e", "f", 7, 8, 9, "g", "h", "i"]
{{{end}}}

## Related content

- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site
{
content: {
docs: {
howto: {
"use-the-built-in-function-list-flattenn-to-flatten-lists": {
page: {
cache: {
code: {
cc: "uDB8YvMpfPhKTjlC0d1OMwsyU7fch7rZDptJJqZlji4="
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site

content: docs: howto: "use-the-built-in-function-list-flattenn-to-flatten-lists": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: Using the built-in function "list.FlattenN" to flatten lists
tags:
- commented cue
authors:
- jpluscplusm
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to use the built-in function `list.FlattenN` to flatten a list
by expanding its list elements down to a specified depth.

{{< code-tabs >}}
{{< code-tab name="file.cue" language="cue" area="top" >}}
package example

import "list"

// lSrc is a list (of lists (of lists ...)), defined below
lSrc: [...]

// eOne's first-level list elements are expanded
eOne: list.FlattenN(lSrc, 1)
// eTwo's first- and second-level list elements are expanded
eTwo: list.FlattenN(lSrc, 2)
// All of eNeg's list elements are expanded, no matter their depth
eNeg: list.FlattenN(lSrc, -1)

lSrc: [
1, 2, 3,
["a", "b", "c"],
[
[4, 5, 6],
[
["d", "e", "f"],
[[7, 8, 9]],
],
],
[[[[["g", "h", "i"]]]]],
]
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}}
$ cue eval
lSrc: [1, 2, 3, ["a", "b", "c"], [[4, 5, 6], [["d", "e", "f"], [[7, 8, 9]]]], [[[[["g", "h", "i"]]]]]]
eOne: [1, 2, 3, "a", "b", "c", [4, 5, 6], [["d", "e", "f"], [[7, 8, 9]]], [[[["g", "h", "i"]]]]]
eTwo: [1, 2, 3, "a", "b", "c", 4, 5, 6, ["d", "e", "f"], [[7, 8, 9]], [[["g", "h", "i"]]]]
eNeg: [1, 2, 3, "a", "b", "c", 4, 5, 6, "d", "e", "f", 7, 8, 9, "g", "h", "i"]
{{< /code-tab >}}
{{< /code-tabs >}}

## Related content

- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package

0 comments on commit 52e8869

Please sign in to comment.