Skip to content

Commit

Permalink
docs/howto: validate an integer value's parity
Browse files Browse the repository at this point in the history
This adds a Commented Cue guide demonstrating how to validate an integer
value's parity.

Examples are provided for the 8 different validation outcomes, based on
the following matrix:
- odd/even
- positive/negative
- valid/invalid

For cue-lang/docs-and-content#97

Preview-Path: /docs/howto/validate-integer-value-parity/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cuelang.org/+/1177968
Reviewed-by: Paul Jolly <paul@myitcv.io>
Reviewed-by: Jonathan Matthews <github@hello.jonathanmatthews.com>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
  • Loading branch information
NoamTD authored and jpluscplusm committed Mar 14, 2024
1 parent 947e3f1 commit e57cd0e
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions content/authors/noamtd/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
displayName: "Noam Dolovich"
---
3 changes: 3 additions & 0 deletions content/authors/noamtd/page.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site

content: authors: noamtd: page: _
55 changes: 55 additions & 0 deletions content/docs/howto/validate-integer-value-parity/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: Validating an integer value's parity
tags:
- commented cue
- validation
authors:
- noamtd
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to validate an integer value's parity.

Two schemas `#Odd` and `#Even` are presented that validate an integer is either odd or even respectively.

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

#Even: num={2 * (div(num, 2))}
#Odd: num={1 + 2*(div(num, 2))}

valid: {
"-27": -27 & #Odd
"-22": -22 & #Even
"42": 42 & #Even
"47": 47 & #Odd
}

invalid: {
"-37": -37 & #Even
"-32": -32 & #Odd
"52": 52 & #Odd
"57": 57 & #Even
}
-- out --
valid: {
"-27": -27
"-22": -22
"42": 42
"47": 47
}
invalid: {
"-37": _|_ // invalid."-37": conflicting values -38 and -37
"-32": _|_ // invalid."-32": conflicting values -31 and -32
"52": _|_ // invalid."52": conflicting values 53 and 52
"57": _|_ // invalid."57": conflicting values 56 and 57
}
{{{end}}}

## Related content

* {{< linkto/related/howto "use-the-built-in-functions-div-mod-quo-rem" >}}
18 changes: 18 additions & 0 deletions content/docs/howto/validate-integer-value-parity/gen_cache.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site
{
content: {
docs: {
howto: {
"validate-integer-value-parity": {
page: {
cache: {
code: {
cc: "vf+j3IRvnHFecRWPM2a/7OODOwIDnMF6Rv3dTjzKZns="
}
}
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions content/docs/howto/validate-integer-value-parity/page.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site

content: docs: howto: "validate-integer-value-parity": page: _
3 changes: 3 additions & 0 deletions hugo/content/en/authors/noamtd/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
displayName: "Noam Dolovich"
---
56 changes: 56 additions & 0 deletions hugo/content/en/docs/howto/validate-integer-value-parity/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: Validating an integer value's parity
tags:
- commented cue
- validation
authors:
- noamtd
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to validate an integer value's parity.

Two schemas `#Odd` and `#Even` are presented that validate an integer is either odd or even respectively.

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

#Even: num={2 * (div(num, 2))}
#Odd: num={1 + 2*(div(num, 2))}

valid: {
"-27": -27 & #Odd
"-22": -22 & #Even
"42": 42 & #Even
"47": 47 & #Odd
}

invalid: {
"-37": -37 & #Even
"-32": -32 & #Odd
"52": 52 & #Odd
"57": 57 & #Even
}
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" area="top-right" type="terminal" codetocopy="Y3VlIGV2YWwgLWlj" >}}
$ cue eval -ic
valid: {
"-27": -27
"-22": -22
"42": 42
"47": 47
}
invalid: {
"-37": _|_ // invalid."-37": conflicting values -38 and -37
"-32": _|_ // invalid."-32": conflicting values -31 and -32
"52": _|_ // invalid."52": conflicting values 53 and 52
"57": _|_ // invalid."57": conflicting values 56 and 57
}
{{< /code-tab >}}
{{< /code-tabs >}}

## Related content

* {{< linkto/related/howto "use-the-built-in-functions-div-mod-quo-rem" >}}

0 comments on commit e57cd0e

Please sign in to comment.