-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 Dispatch-Trailer: {"type":"trybot","CL":1174439,"patchset":14,"ref":"refs/changes/39/1174439/14","targetBranch":"alpha"}
- Loading branch information
1 parent
74b3c8b
commit 3982c7a
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
content/docs/howto/use-the-built-in-function-list-flattenn-to-flatten-lists/en.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
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`](https://pkg.go.dev/cuelang.org/go/pkg/list#FlattenN) | ||
to flatten a list by expanding its list elements by a specified depth. | ||
|
||
{{{with code "en" "cc"}}} | ||
#location top bottom | ||
|
||
exec cue eval | ||
cmp stdout out | ||
-- 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"]]]]], | ||
] | ||
-- out -- | ||
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"] | ||
{{{end}}} | ||
|
||
## Related content | ||
|
||
- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package |
18 changes: 18 additions & 0 deletions
18
content/docs/howto/use-the-built-in-function-list-flattenn-to-flatten-lists/gen_cache.cue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "AhHhGLP8qj1FskfTMcu85QL032IzKy8voDJ9XuA68OE=" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
content/docs/howto/use-the-built-in-function-list-flattenn-to-flatten-lists/page.cue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} |
56 changes: 56 additions & 0 deletions
56
...en/docs/howto/use-the-built-in-function-list-flattenn-to-flatten-lists/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
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`](https://pkg.go.dev/cuelang.org/go/pkg/list#FlattenN) | ||
to flatten a list by expanding its list elements by a specified depth. | ||
|
||
{{< code-tabs >}} | ||
{{< code-tab name="file.cue" language="cue" area="top" >}} | ||
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"]]]]], | ||
] | ||
{{< /code-tab >}} | ||
{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}} | ||
$ 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"] | ||
{{< /code-tab >}} | ||
{{< /code-tabs >}} | ||
|
||
## Related content | ||
|
||
- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package |