Expr.doc: add section about intervals

This commit is contained in:
2026-08-01 17:40:02 +02:00
parent 481074d104
commit e2b0a43640
+47 -3
View File
@@ -506,6 +506,50 @@ Currently, boolean operations are evaluated using _short cut evaluation_. This m
TIP: `ecli` provides the _ctrl()_ function that allows to change this behaviour.
====
=== Intervals
_Expr_ supports intervals, which are a special kind of data type. An interval is a range *_open on the right_* of integer values between a lower and an upper bound. The lower and upper bounds can be specified in any order, i.e. the lower bound can be greater than the upper bound. In this case, the interval is considered _descending_.
Unlike arrays, intervals are not collections of values, but rather a compact representation of a range of values. And unlike other programming languages that separate lower and upper bounds by a colonn, _Expr_ separates them by a double dot symbol `..`.
NOTE: Intervals are _open on the right_, which means that the upper bound is not included in the interval. For example, the interval `1..5` contains the values 1, 2, 3, and 4, but not 5.
_Expr_'s intervals also support step values, which are optionally specified by a third integer value after the upper bound, separated by another doble dot symbol `..`. In this case, the interval represents a range of values starting from the lower bound, up to the upper bound, with a step increment equal to the specified step value. If the step value is not specified, it defaults to 1.
IMPORTANT: The step value must be a positive integer, despite the fact that the lower bound is greater than the upper bound. If it is negative or zero, an error is returned.
.Interval literal syntax
====
*_interval_* = [_lower-bound_] "**..**" _upper-bound_ ["**..**" _step_]
_lower-bound_ = _integer-expr_ +
_upper-bound_ = _integer-expr_ +
_step_ = _positive-integer-expr_
====
.Examples
`>>>` [blue]`3..5` +
[green]`3..5..1`
`>>>` [blue]`5..3` +
[green]`5..3..1`
`>>>` [blue]`..4` +
[green]`0..4..1`
`>>>` [blue]`4..` +
[green]`4..4..1`
`>>>` [blue]`-1..-4` +
[green]`-1..-4..1`
`>>>` [blue]`-1..-4..-1` +
[red]`Eval Error: [1:8] invalid interval specification: step must be positive`
`>>>` [blue]`r = -1..-4` [gray]_// assign interval to variable r_ +
[green]`-1..-4..1`
Main usages of intervals are to generate slices of arrays and to check if a value is in an interval.
=== Arrays
_Expr_ supports arrays of mixed-type values, also specified by normal expressions. Internally, _Expr_'s arrays are Go slices.
@@ -513,7 +557,7 @@ _Expr_ supports arrays of mixed-type values, also specified by normal expression
====
*_array_* = _empty-array_ | _non-empty-array_ +
_empty-array_ = "**[]**" +
_non-empty-array_ = "**[**" _any-value_ {"**,**" _any-value_} "**]**" +
_non-empty-array_ = "**[**" _any-value_ {"**,**" _any-value_} "**]**"
====
.Examples
@@ -559,7 +603,7 @@ Array's items can be accessed using the index `[]` operator.
.Sub-array (or slice of array) syntax
====
*_slice_* = _array-expr_ "**[**" _integer-expr_ "**:**" _integer-expr_ "**]**"
*_slice_* = _array-expr_ "**[**" _interval-expr_ "**]**"
====
.Examples: Getting items from arrays
@@ -569,7 +613,7 @@ Array's items can be accessed using the index `[]` operator.
`>>>` [blue]`index=2; ["a", "b", "c", "d"][index]` +
[green]`c`
`>>>` [blue]`["a", "b", "c", "d"][2:]` +
`>>>` [blue]`["a", "b", "c", "d"][2..4]` +
[green]`["c", "d"]`
`>>>` [blue]`array=[1,2,3]; array[1]` +