Cue has the typical math operations for numbers.
Multiplication also works for strings and lists.
math-ops.cue
// mathematical!n1: 3+2n2: 3-2n3: 3*2n4: 3/2// Truncated divisionq: quo(-5, 2)r: rem(-5, 2)// Euclidean divisiond: div(-5, 2)m: mod(-5, 2)// math on strings and listss: 3*"for he's a jolly good fellow\n"+"which nobody can deny"l: 5* ["eye"]
cue eval math-ops.cue
n1: 5n2: 1n3: 6n4: 1.5q: -2r: -1d: -3m: 1s: """ for he's a jolly good fellow for he's a jolly good fellow for he's a jolly good fellow which nobody can deny """l: ["eye", "eye", "eye", "eye", "eye"]
Additional math operations can be found in the Cue’s
math package
.
Comparison Operations
Cue has the expected relative comparison operators and semantics.
Equality checks are handled by value unification.
For more complicated scenarios, you can use the
text/template
package.
You can also interpolate field names. (as we will see shortly)
List Comprehensions
Cue has list comprehensions to dynamically create lists.
You can iterate over both lists and struct fields.
The form is [ for key, val in <iterable> [condition] { production } ]
* key is the index for lists and the label for fields
list-comp.cue
nums: [1, 2, 3, 4, 5, 6]sqrd: [ for _, n in nums {n * n}]even: [ for _, n in nums ifmod(n, 2) ==0 {n}]listOfStructs: [ for p, n in nums { pos: p val: n}]extractVals: [ for p, S in listOfStructs {S.val}]