Skip to content

Latest commit

 

History

History
261 lines (204 loc) · 5.26 KB

Language-Definition.md

File metadata and controls

261 lines (204 loc) · 5.26 KB

Language Definition

Literals

Comment /* */ or //
Boolean true, false
Integer 42, 0x2A
Float 0.5, .5
String "foo", 'bar'
Array [1, 2, 3]
Map {a: 1, b: 2, c: 3}
Nil nil

Operators

Arithmetic +, -, *, /, % (modulus), ^ or ** (exponent)
Comparison ==, !=, <, >, <=, >=
Logical not or !, and or &&, or or ||
Conditional ?: (ternary), ?? (nil coalescing)
Membership [], ., ?., in
String + (concatenation), contains, startsWith, endsWith
Regex matches
Range ..
Slice [:]

Examples:

user.Age in 18..45 and user.Name not in ["admin", "root"]
foo matches "^[A-Z].*"

Membership Operator

Fields of structs and items of maps can be accessed with . operator or [] operator. Elements of arrays and slices can be accessed with [] operator. Negative indices are supported with -1 being the last element.

The in operator can be used to check if an item is in an array or a map.

user.Name in list["available-names"]

Optional chaining

The ?. operator can be used to access a field of a struct or an item of a map without checking if the struct or the map is nil. If the struct or the map is nil, the result of the expression is nil.

author?.User?.Name

Nil coalescing

The ?? operator can be used to return the left-hand side if it is not nil, otherwise the right-hand side is returned.

author?.User?.Name ?? "Anonymous"

Slice Operator

The slice operator [:] can be used to access a slice of an array.

For example, variable array is [1, 2, 3, 4, 5]:

array[1:4] == [2, 3, 4]
array[1:-1] == [2, 3, 4]
array[:3] == [1, 2, 3]
array[3:] == [4, 5]
array[:] == array

Built-in Functions

all(array, predicate)

Returns true if all elements satisfies the predicate. If the array is empty, returns true.

all(Tweets, {.Size < 280})

any(array, predicate)

Returns true if any elements satisfies the predicate. If the array is empty, returns false.

one(array, predicate)

Returns true if exactly one element satisfies the predicate. If the array is empty, returns false.

one(Participants, {.Winner})

none(array, predicate)

Returns true if all elements does not satisfy the predicate. If the array is empty, returns true.

map(array, predicate)

Returns new array by applying the predicate to each element of the array.

filter(array, predicate)

Returns new array by filtering elements of the array by predicate.

count(array, predicate)

Returns the number of elements what satisfies the predicate. Equivalent to:

len(filter(array, predicate))

len(v)

Returns the length of an array, a map or a string.

abs(v)

Returns the absolute value of a number.

int(v)

Returns the integer value of a number or a string.

int("123") == 123

float(v)

Returns the float value of a number or a string.

Predicate

The predicate is an expression that accepts a single argument. To access the argument use the # symbol.

map(0..9, {# / 2})

If items of the array is a struct or a map, it is possible to access fields with omitted # symbol (#.Value becomes .Value).

filter(Tweets, {len(.Value) > 280})

Braces { } can be omitted:

filter(Tweets, len(.Value) > 280)