-
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.
docs/howto: combine multiple if clauses
This adds a Commented CUE that shows several examples of combining the same set of if clauses. Unification at the top-level of the evaluation is used in order to keep the scope of the examples as general as possible, so that the reader can more easily extract and use their lessons in both field and list comprehensions - without having to first extract them from a comprehension type that they're /not/ placed inside. Closes cue-lang/docs-and-content#185. Preview-Path: /docs/howto/combine-multiple-if-clauses/ Signed-off-by: Jonathan Matthews <[email protected]> Change-Id: I899019354b0b31c66dcff8a2458812011499f384 Dispatch-Trailer: {"type":"trybot","CL":1203075,"patchset":1,"ref":"refs/changes/75/1203075/1","targetBranch":"master"}
- Loading branch information
1 parent
7589a73
commit 735cf31
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
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,66 @@ | ||
--- | ||
title: 'Combining multiple "if" clauses' | ||
authors: [jpluscplusm] | ||
toc_hide: true | ||
tags: [commented cue] | ||
--- | ||
|
||
This [Commented CUE]({{< relref "docs/howto/about-commented-cue-guides" >}}) | ||
demonstrates how to combine more than one `if` clause. This can be useful when | ||
your decison to include data needs to be based on multiple conditions. | ||
|
||
{{{with code "en" "cc"}}} | ||
exec cue export | ||
cmp stdout out | ||
-- file.cue -- | ||
package example | ||
|
||
// All these examples represent identical | ||
// combinations of the same set of "if" clauses. | ||
|
||
// All conditions on separate lines: | ||
if #A < 100 | ||
if #B > 100 | ||
if (#C & number) != _|_ | ||
if #A < #C | ||
if #B < #C { | ||
success: true | ||
} | ||
|
||
// Some conditions on the same line and some on | ||
// separate lines: | ||
if #A < 100 if #B > 100 | ||
if (#C & number) != _|_ | ||
if #A < #C if #B < #C { | ||
success: true | ||
} | ||
|
||
// All conditions on the same line: | ||
if #A < 100 if #B > 100 if (#C & number) != _|_ if #A < #C if #B < #C { | ||
success: true | ||
} | ||
|
||
#A: 42 | ||
#B: 137 | ||
#C: 10101 | ||
-- out -- | ||
{ | ||
"success": true | ||
} | ||
{{{end}}} | ||
|
||
{{<info>}} | ||
Because `if` clauses can be used in | ||
[list comprehensions]({{< relref "/docs/tour/expressions/listcomp" >}}) | ||
and | ||
[field comprehensions]({{< relref "/docs/tour/expressions/fieldcomp" >}}), | ||
the | ||
[conditional field]({{< relref "/docs/tour/expressions/conditional" >}}) | ||
examples demonstrated above are valid in both contexts. | ||
{{</info>}} | ||
|
||
## Related content | ||
|
||
- {{< linkto/related/tour "expressions/conditional" >}} | ||
- {{< linkto/related/tour "expressions/listcomp" >}} | ||
- {{< linkto/related/tour "expressions/fieldcomp" >}} |
18 changes: 18 additions & 0 deletions
18
content/docs/howto/combine-multiple-if-clauses/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: { | ||
"combine-multiple-if-clauses": { | ||
page: { | ||
cache: { | ||
code: { | ||
cc: "8/NaB+qCrVCbjIoj3uoriSzwKl8fmeyoJebohy2SLW4=" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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: "combine-multiple-if-clauses": page: _ |
67 changes: 67 additions & 0 deletions
67
hugo/content/en/docs/howto/combine-multiple-if-clauses/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,67 @@ | ||
--- | ||
title: 'Combining multiple "if" clauses' | ||
authors: [jpluscplusm] | ||
toc_hide: true | ||
tags: [commented cue] | ||
--- | ||
|
||
This [Commented CUE]({{< relref "docs/howto/about-commented-cue-guides" >}}) | ||
demonstrates how to combine more than one `if` clause. This can be useful when | ||
your decison to include data needs to be based on multiple conditions. | ||
|
||
{{< code-tabs >}} | ||
{{< code-tab name="file.cue" language="cue" area="top-left" >}} | ||
package example | ||
|
||
// All these examples represent identical | ||
// combinations of the same set of "if" clauses. | ||
|
||
// All conditions on separate lines: | ||
if #A < 100 | ||
if #B > 100 | ||
if (#C & number) != _|_ | ||
if #A < #C | ||
if #B < #C { | ||
success: true | ||
} | ||
|
||
// Some conditions on the same line and some on | ||
// separate lines: | ||
if #A < 100 if #B > 100 | ||
if (#C & number) != _|_ | ||
if #A < #C if #B < #C { | ||
success: true | ||
} | ||
|
||
// All conditions on the same line: | ||
if #A < 100 if #B > 100 if (#C & number) != _|_ if #A < #C if #B < #C { | ||
success: true | ||
} | ||
|
||
#A: 42 | ||
#B: 137 | ||
#C: 10101 | ||
{{< /code-tab >}} | ||
{{< code-tab name="TERMINAL" language="" area="top-right" type="terminal" codetocopy="Y3VlIGV4cG9ydA==" >}} | ||
$ cue export | ||
{ | ||
"success": true | ||
} | ||
{{< /code-tab >}} | ||
{{< /code-tabs >}} | ||
|
||
{{<info>}} | ||
Because `if` clauses can be used in | ||
[list comprehensions]({{< relref "/docs/tour/expressions/listcomp" >}}) | ||
and | ||
[field comprehensions]({{< relref "/docs/tour/expressions/fieldcomp" >}}), | ||
the | ||
[conditional field]({{< relref "/docs/tour/expressions/conditional" >}}) | ||
examples demonstrated above are valid in both contexts. | ||
{{</info>}} | ||
|
||
## Related content | ||
|
||
- {{< linkto/related/tour "expressions/conditional" >}} | ||
- {{< linkto/related/tour "expressions/listcomp" >}} | ||
- {{< linkto/related/tour "expressions/fieldcomp" >}} |