Skip to content

Commit

Permalink
Update Language-Definition.md
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jan 22, 2023
1 parent 76cf095 commit 5a2e18d
Showing 1 changed file with 57 additions and 51 deletions.
108 changes: 57 additions & 51 deletions docs/Language-Definition.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
# Language Definition

**Expr** is an expression evaluation language for Go.
<table>
<tr><th colspan="2">Built-in Functions</th></tr>
<tr>
<td>
<a href="#allarray-predicate">all()</a><br>
<a href="#anyarray-predicate">any()</a><br>
<a href="#lenarray-predicate">one()</a><br>
<a href="#nonearray-predicate">none()</a><br>
</td>
<td>
<a href="#lenv">len()</a><br>
<a href="#maparray-predicate">map()</a><br>
<a href="#filterarray-predicate">filter()</a><br>
<a href="#countarray-predicate">count()</a><br>
</td>
</tr>
</table>

## Supported Literals

Expand All @@ -15,7 +31,8 @@ The package supports:

## Digit separators

Integer literals may contain digit separators to allow digit grouping into more legible forms.
Integer literals may contain digit separators to allow digit grouping into more
legible forms.

Example:

Expand All @@ -25,7 +42,8 @@ Example:

## Fields

Struct fields and map elements can be accessed by using the `.` or the `[]` syntax.
Struct fields and map elements can be accessed by using the `.` or the `[]`
syntax.

```js
foo.Field
Expand Down Expand Up @@ -107,7 +125,7 @@ user.Group in ["human_resources", "marketing"]
"foo" in {foo: 1, bar: 2}
```

### Numeric Operators
### Range Operator

* `..` (range)

Expand All @@ -123,7 +141,24 @@ The range is inclusive:
1..3 == [1, 2, 3]
```

### Ternary Operators
### Slice Operator

* `array[:]` (slice)

Slices can work with arrays or strings.

Example:

Variable `array` is `[1,2,3,4,5]`.

```js
array[1:4] == [2,3,4]
array[:3] == [1,2,3]
array[3:] == [4,5]
array[:] == array
```

### Ternary Operator

* `foo ? 'yes' : 'no'`

Expand All @@ -135,98 +170,69 @@ user.Age > 30 ? "mature" : "immature"

## Built-in Functions

<table>
<tr>
<td>
<a href="#allarray-predicate">all()</a><br>
<a href="#anyarray-predicate">any()</a><br>
<a href="#lenarray-predicate">one()</a><br>
<a href="#nonearray-predicate">none()</a><br>
</td>
<td>
<a href="#lenv">len()</a><br>
<a href="#maparray-closure">map()</a><br>
<a href="#filterarray-predicate">filter()</a><br>
<a href="#countarray-predicate">count()</a><br>
</td>
</tr>
</table>


### `all(array, predicate)`

Returns **true** if all elements satisfies the predicate (or if the array is empty).
Returns **true** if all elements satisfies the [predicate](#predicate).
If the array is empty, returns **true**.

```js
all(Tweets, {.Size < 280})
```

### `any(array, predicate)`

Returns **true** if any elements satisfies the predicate. If the array is empty, returns **false**.
Returns **true** if any elements satisfies the [predicate](#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**.
Returns **true** if _exactly one_ element satisfies the [predicate](#predicate).
If the array is empty, returns **false**.

```js
one(Participants, {.Winner})
```

### `none(array, predicate)`

Returns **true** if _all elements does not_ satisfy the predicate. If the array is empty, returns **true**.
Returns **true** if _all elements does not_ satisfy the [predicate](#predicate).
If the array is empty, returns **true**.

### `len(v)`

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

### `map(array, closure)`
### `map(array, predicate)`

Returns new array by applying the closure to each element of the array.
Returns new array by applying the [predicate](#predicate) to each element of
the array.

### `filter(array, predicate)`

Returns new array by filtering elements of the array by predicate.
Returns new array by filtering elements of the array by [predicate](#predicate).

### `count(array, predicate)`

Returns the number of elements what satisfies the predicate. Equivalent to:
Returns the number of elements what satisfies the [predicate](#predicate).
Equivalent to:

```js
len(filter(array, predicate))
```

## Closures
## Predicate

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

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

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

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

## Slices

* `array[:]` (slice)

Slices can work with arrays or strings.

Example:

Variable `array` is `[1,2,3,4,5]`.

```js
array[1:4] == [2,3,4]
array[:3] == [1,2,3]
array[3:] == [4,5]
array[:] == array
```

0 comments on commit 5a2e18d

Please sign in to comment.