Skip to content

Commit

Permalink
docs/howto: combine multiple if clauses
Browse files Browse the repository at this point in the history
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
jpluscplusm authored and cueckoo committed Oct 24, 2024
1 parent 7589a73 commit 735cf31
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
66 changes: 66 additions & 0 deletions content/docs/howto/combine-multiple-if-clauses/en.md
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 content/docs/howto/combine-multiple-if-clauses/gen_cache.cue
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="
}
}
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions content/docs/howto/combine-multiple-if-clauses/page.cue
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 hugo/content/en/docs/howto/combine-multiple-if-clauses/index.md
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" >}}

0 comments on commit 735cf31

Please sign in to comment.