Skip to content

Commit

Permalink
docs/howto: conditionally include list elements
Browse files Browse the repository at this point in the history
This adds a guide that demonstrates how to include elements in a list
conditionally.

Closes cue-lang/docs-and-content#117

Preview-Path: /docs/howto/conditionally-include-list-elements/
Signed-off-by: Jonathan Matthews <[email protected]>
Change-Id: I4eea7a9294a189f8a7c160eb16f06736f7afa74a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cuelang.org/+/1197391
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
jpluscplusm committed Jul 23, 2024
1 parent 8a12333 commit b5df5d4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
47 changes: 47 additions & 0 deletions content/docs/howto/conditionally-include-list-elements/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: Conditionally including elements in a list
authors: [jpluscplusm]
toc_hide: true
tags: [commented cue]
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to include elements in a list based on some testable condition.

{{{with code "en" "cc"}}}
exec cue export
cmp stdout out
-- example.cue --
package example

#a: "yes"

A: [
1,
2,

// Include a single element using this form:
if #a == "yes" {
3
},

// Include multiple elements using this form,
// which only tests the condition once:
if #a == "yes" for e in [
4,
5,
6,
] {e},
]
-- out --
{
"A": [
1,
2,
3,
4,
5,
6
]
}
{{{end}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site
{
content: {
docs: {
howto: {
"conditionally-include-list-elements": {
page: {
cache: {
code: {
cc: "7HIdQ5DAaafYiGotXzpD2y8Dvs1Xs3PlUfim+NB9dW4="
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site

content: docs: howto: "conditionally-include-list-elements": page: _
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: Conditionally including elements in a list
authors: [jpluscplusm]
toc_hide: true
tags: [commented cue]
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to include elements in a list based on some testable condition.

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

#a: "yes"

A: [
1,
2,

// Include a single element using this form:
if #a == "yes" {
3
},

// Include multiple elements using this form,
// which only tests the condition once:
if #a == "yes" for e in [
4,
5,
6,
] {e},
]
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" area="top-right" type="terminal" codetocopy="Y3VlIGV4cG9ydA==" >}}
$ cue export
{
"A": [
1,
2,
3,
4,
5,
6
]
}
{{< /code-tab >}}
{{< /code-tabs >}}

0 comments on commit b5df5d4

Please sign in to comment.