The Function Pattern

You can use Cue definitions or structs to create function like objects. It is typical to have an input and output schema, and then you are free to do whatever else you like in between them.

There is a proposal to simplify this pattern with official syntax. See the beginning of the core builtin extensions for more details.


package function

import "strings"

// An input schema
#Input: {
	count: int
	msg:   string

// An output schema
#Output: {
	val: string

#Transform: {
	// Input for the caller
	X1="in": #Input
	// output for the caller
	out: #Output

	// intermediate fields
	_upper: strings.ToUpper(X1.msg)
	_msg:   strings.Join([_upper]*X1.count, " ")

	// set output
	out: val: _msg

// Call transform
result: #Transform & {in: {msg: "ra", count: 3}}

cue eval function.cue -e result

in: {
    count: 3
    msg:   "ra"
out: {
    val: "RA RA RA"
We'll never share your email with anyone else.
2024 Hofstadter, Inc