Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs/howto/ensure-int-is-odd-or-even: Ensuring that an integer value is either odd or even #97

Closed
jpluscplusm opened this issue Mar 7, 2024 · 3 comments
Labels
content idea Idea for a new piece of content good first issue Good for newcomers howto For content that in the "howto" diataxis quadrant

Comments

@jpluscplusm
Copy link
Collaborator

jpluscplusm commented Mar 7, 2024

exec cue eval -i file.cue
cmp stdout out
-- file.cue --
#even: num={2 * (__div(num, 2))}
#odd:  num={2*(__div(num, 2)) + 1}

a: 56 & #even
b: 97 & #even
c: 56 & #odd
d: 97 & #odd
-- out --
#even: 2 * div(num, 2)
#odd:  2*div(num, 2) + 1
a:     56
b:     _|_ // b: conflicting values 96 and 97
c:     _|_ // c: conflicting values 57 and 56
d:     97

Can, of course, be expanded to check for multiples of any integer, not just 2; but the resulting page title would get a little wordy.

This also works:

#even: math.Trunc(x/2) * 2

... but doing the same for #odd is more fiddly, as it requires responding to the sign of the original number. i.e. the trailing +1 needs to be -1 if the number is negative. Not /hard/, just inelegant, relative to the first solution, above.

@jpluscplusm jpluscplusm added content idea Idea for a new piece of content good first issue Good for newcomers howto For content that in the "howto" diataxis quadrant labels Mar 7, 2024
@jpluscplusm
Copy link
Collaborator Author

It'd be worth checking in on cue-lang/cue#2926 to see if a standard-library-based solution could also be demonstrated.

cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 9, 2024
This adds a Commented Cue guide demonstrating how to ensure that a given number is even/odd.
Examples are provided for the 4 different validation outcomes.

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

Preview-Path: /docs/howto/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 9, 2024
This adds a Commented Cue guide demonstrating how to ensure that a given number is even/odd.
Examples are provided for the 4 different validation outcomes.

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

Preview-Path: /docs/howto/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 9, 2024
This adds a Commented Cue guide demonstrating how to ensure that a given number is even/odd.
Examples are provided for the 4 possible different validation outcomes.

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

Preview-Path: /docs/howto/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 12, 2024
This adds a Commented Cue guide demonstrating how to ensure that a given number is even/odd.
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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 12, 2024
This adds a Commented Cue guide demonstrating how to ensure that a given number is even/odd.
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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 12, 2024
This adds a Commented Cue guide demonstrating how to ensure
that a given number is even/odd.

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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 12, 2024
This adds a Commented Cue guide demonstrating how to ensure
that a given number is even/odd.

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

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

Preview-Path: /docs/howto/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 12, 2024
This adds a Commented Cue guide demonstrating how to ensure
that a given number is even/odd.

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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 12, 2024
This adds a Commented Cue guide demonstrating how to ensure
that a given integer is even/odd.

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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
@jpluscplusm
Copy link
Collaborator Author

cueckoo pushed a commit to cue-lang/cuelang.org-trybot that referenced this issue Mar 12, 2024
This adds a Commented Cue guide demonstrating how to ensure
that a given integer is even/odd.

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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Dispatch-Trailer: {"type":"trybot","CL":1177968,"patchset":10,"ref":"refs/changes/68/1177968/10","targetBranch":"alpha"}
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 14, 2024
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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 14, 2024
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/ensure-int-is-odd-or-even/
Change-Id: Ibc7a31dcbbb13a8d155f30731c961691e47d79ed
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 14, 2024
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>
cueckoo pushed a commit to cue-lang/cuelang.org-trybot that referenced this issue Mar 14, 2024
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>
Dispatch-Trailer: {"type":"trybot","CL":1177968,"patchset":13,"ref":"refs/changes/68/1177968/13","targetBranch":"alpha"}
cueckoo pushed a commit to cue-lang/cuelang.org that referenced this issue Mar 14, 2024
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>
@jpluscplusm
Copy link
Collaborator Author

Closed via cue-lang/cuelang.org@e57cd0e, and now published at https://alpha.cuelang.org/docs/howto/validate-integer-value-parity/.

Many thanks for writing this, @NoamTD!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content idea Idea for a new piece of content good first issue Good for newcomers howto For content that in the "howto" diataxis quadrant
Projects
Status: Done
Development

No branches or pull requests

1 participant