From 39976715abb7440952ecfb1eaf624c34c0a88548 Mon Sep 17 00:00:00 2001 From: Jonathan Matthews Date: Wed, 10 Jan 2024 14:55:44 +0000 Subject: [PATCH] docs/howto: use list.Contains to search in a list This adds a Commented CUE guide demonstrating how to use list.Contains to report if value is found in a list. The function call is initially demonstrated with an inline example, and then in slightly more structured form with 2x2 simple and 1x2 composite examples. It's important to include the composite example to show the range of options that /don't/ trigger a positive result - i.e. what CUE /doesn't/ consider a match by this function's standards. Preview-Path: /docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/ Signed-off-by: Jonathan Matthews Change-Id: I5db7e64b786f239d031cfec00e4ba50db0ef8e0f Dispatch-Trailer: {"type":"trybot","CL":1174384,"patchset":4,"ref":"refs/changes/84/1174384/4","targetBranch":"alpha"} --- .../en.md | 111 ++++++++++++++++++ .../gen_cache.cue | 18 +++ .../page.cue | 3 + .../index.md | 110 +++++++++++++++++ 4 files changed, 242 insertions(+) create mode 100644 content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/en.md create mode 100644 content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/gen_cache.cue create mode 100644 content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/page.cue create mode 100644 hugo/content/en/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/index.md diff --git a/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/en.md b/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/en.md new file mode 100644 index 000000000..163239497 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/en.md @@ -0,0 +1,111 @@ +--- +title: Using the built-in function "list.Contains" to report if a value is contained in a list +tags: +- commented cue +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`list.Contains`](https://pkg.go.dev/cuelang.org/go/pkg/list#Contains) +to report if a simple or composite value is contained in a list. + +{{{with code "en" "cc"}}} +#location top bottom + +exec cue eval +cmp stdout out +-- file.cue -- +package example + +import "list" + +inline: list.Contains([1, 2, "3", 4, "five"], 4) + +numbers: { + haystack1: list.Contains(_haystack1, _needle) + haystack2: list.Contains(_haystack2, _needle) + + _needle: 42 + // _haystack1 does *not* contain the needle. + _haystack1: [-10, 0, 10, 20, 30, 40, 50] + // _haystack2 *does* contain the needle. + _haystack2: [-10, 0, 42, 20, 30, 42, 50] +} + +strings: { + haystack1: list.Contains(_haystack1, _needle) + haystack2: list.Contains(_haystack2, _needle) + + _needle: "A Needle" + // _haystack1 does *not* contain the needle. + _haystack1: ["some hay", "more hay", "nothing but hay"] + // _haystack2 *does* contain the needle. + _haystack2: ["some hay", "more hay", "A Needle", "even more hay"] +} + +composite: { + haystack1: list.Contains(_haystack1, _needle) + haystack2: list.Contains(_haystack2, _needle) + + _needle: { + a: 42 + b: c: "A Needle" + } + // _haystack1 does *not* contain the needle. + _haystack1: [ + { + a: 42 + b: c: "a needle" + }, + { + a: 42 + b: c: "A Needle" + b: d: "An extra field" + }, + { + a: 42 + B: c: "A Needle" + }, + { + a: 42 + b: c: "A Needle": ".. as a key, not a value" + }, + { + a: 42.0 + b: c: "A Needle" + }, + ] + // _haystack2 *does* contain the needle. + _haystack2: [ + -10, + 0, + "some hay", + "more hay", + { + a: 42 + b: c: "A Needle" + }, + ] +} +-- out -- +inline: true +numbers: { + haystack1: false + haystack2: true +} +strings: { + haystack1: false + haystack2: true +} +composite: { + haystack1: false + haystack2: true +} +{{{end}}} + +## Related content + +- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package diff --git a/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/gen_cache.cue b/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/gen_cache.cue new file mode 100644 index 000000000..fbea4d97f --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/gen_cache.cue @@ -0,0 +1,18 @@ +package site +{ + content: { + docs: { + howto: { + "use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list": { + page: { + cache: { + code: { + cc: "NKcfxC3yjQXOUiE7eOgkUdtMBiENqjOOhzLbOW9VELM=" + } + } + } + } + } + } + } +} diff --git a/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/page.cue b/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/page.cue new file mode 100644 index 000000000..a511da125 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/page.cue @@ -0,0 +1,3 @@ +package site + +content: docs: howto: "use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list": {} diff --git a/hugo/content/en/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/index.md b/hugo/content/en/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/index.md new file mode 100644 index 000000000..03ec9130c --- /dev/null +++ b/hugo/content/en/docs/howto/use-the-built-in-function-list-contains-to-report-if-a-value-is-in-a-list/index.md @@ -0,0 +1,110 @@ +--- +title: Using the built-in function "list.Contains" to report if a value is contained in a list +tags: +- commented cue +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`list.Contains`](https://pkg.go.dev/cuelang.org/go/pkg/list#Contains) +to report if a simple or composite value is contained in a list. + +{{< code-tabs >}} +{{< code-tab name="file.cue" language="cue" area="top" >}} +package example + +import "list" + +inline: list.Contains([1, 2, "3", 4, "five"], 4) + +numbers: { + haystack1: list.Contains(_haystack1, _needle) + haystack2: list.Contains(_haystack2, _needle) + + _needle: 42 + // _haystack1 does *not* contain the needle. + _haystack1: [-10, 0, 10, 20, 30, 40, 50] + // _haystack2 *does* contain the needle. + _haystack2: [-10, 0, 42, 20, 30, 42, 50] +} + +strings: { + haystack1: list.Contains(_haystack1, _needle) + haystack2: list.Contains(_haystack2, _needle) + + _needle: "A Needle" + // _haystack1 does *not* contain the needle. + _haystack1: ["some hay", "more hay", "nothing but hay"] + // _haystack2 *does* contain the needle. + _haystack2: ["some hay", "more hay", "A Needle", "even more hay"] +} + +composite: { + haystack1: list.Contains(_haystack1, _needle) + haystack2: list.Contains(_haystack2, _needle) + + _needle: { + a: 42 + b: c: "A Needle" + } + // _haystack1 does *not* contain the needle. + _haystack1: [ + { + a: 42 + b: c: "a needle" + }, + { + a: 42 + b: c: "A Needle" + b: d: "An extra field" + }, + { + a: 42 + B: c: "A Needle" + }, + { + a: 42 + b: c: "A Needle": ".. as a key, not a value" + }, + { + a: 42.0 + b: c: "A Needle" + }, + ] + // _haystack2 *does* contain the needle. + _haystack2: [ + -10, + 0, + "some hay", + "more hay", + { + a: 42 + b: c: "A Needle" + }, + ] +} +{{< /code-tab >}} +{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}} +$ cue eval +inline: true +numbers: { + haystack1: false + haystack2: true +} +strings: { + haystack1: false + haystack2: true +} +composite: { + haystack1: false + haystack2: true +} +{{< /code-tab >}} +{{< /code-tabs >}} + +## Related content + +- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package