Skip to content

Commit b427e16

Browse files
committed
Update Language-Definition.md
1 parent e3bfec7 commit b427e16

File tree

1 file changed

+152
-3
lines changed

1 file changed

+152
-3
lines changed

docs/Language-Definition.md

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,85 @@ If the array is empty, returns **true**.
220220
Returns new array by applying the [predicate](#predicate) to each element of
221221
the array.
222222

223+
```
224+
map(Tweets, {.Size})
225+
```
226+
223227
### filter(array, predicate)
224228

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

231+
```
232+
filter(users, .Name startsWith "J")
233+
```
234+
235+
### find(array, predicate)
236+
237+
Finds the first element in an array that satisfies the [predicate](#predicate).
238+
239+
```expr
240+
find([1, 2, 3, 4], # > 2) == 3
241+
```
242+
243+
### findIndex(array, predicate)
244+
245+
Finds the index of the first element in an array that satisfies the [predicate](#predicate).
246+
247+
```expr
248+
findIndex([1, 2, 3, 4], # > 2) == 2
249+
```
250+
251+
### findLast(array, predicate)
252+
253+
Finds the last element in an array that satisfies the [predicate](#predicate).
254+
255+
```expr
256+
findLast([1, 2, 3, 4], # > 2) == 4
257+
```
258+
259+
### findLastIndex(array, predicate)
260+
261+
Finds the index of the last element in an array that satisfies the [predicate](#predicate).
262+
263+
```expr
264+
findLastIndex([1, 2, 3, 4], # > 2) == 3
265+
```
266+
267+
### groupBy(array, predicate)
268+
269+
Groups the elements of an array by the result of the [predicate](#predicate).
270+
271+
```expr
272+
groupBy(users, .Age)
273+
```
274+
227275
### count(array, predicate)
228276

229277
Returns the number of elements what satisfies the [predicate](#predicate).
278+
230279
Equivalent to:
231280

232281
```expr
233282
len(filter(array, predicate))
234283
```
235284

285+
### reduce(array, predicate[, initialValue])
286+
287+
Applies a predicate to each element in the array, reducing the array to a single value.
288+
Optional `initialValue` argument can be used to specify the initial value of the accumulator.
289+
If `initialValue` is not given, the first element of the array is used as the initial value.
290+
291+
Following variables are available in the predicate:
292+
293+
- `#` - the current element
294+
- `#acc` - the accumulator
295+
- `#index` - the index of the current element
296+
297+
```expr
298+
reduce(1..9, #acc + #)
299+
reduce(1..9, #acc + #, 0)
300+
```
301+
236302
### len(v)
237303

238304
Returns the length of an array, a map or a string.
@@ -243,7 +309,6 @@ Returns the type of the given value `v`.
243309
Returns on of the following types: `nil`, `bool`, `int`, `uint`, `float`, `string`, `array`, `map`.
244310
For named types and structs, the type name is returned.
245311

246-
247312
```expr
248313
type(42) == "int"
249314
type("hello") == "string"
@@ -408,6 +473,30 @@ Returns the minimum of the two values `v1` and `v2`.
408473
min(5, 7) == 5
409474
```
410475

476+
### sum(array)
477+
478+
Returns the sum of all numbers in the array.
479+
480+
```expr
481+
sum([1, 2, 3]) == 6
482+
```
483+
484+
### mean(array)
485+
486+
Returns the average of all numbers in the array.
487+
488+
```expr
489+
mean([1, 2, 3]) == 2.0
490+
```
491+
492+
### median(array)
493+
494+
Returns the median of all numbers in the array.
495+
496+
```expr
497+
median([1, 2, 3]) == 2.0
498+
```
499+
411500
### toJSON(v)
412501

413502
Converts the given value `v` to its JSON string representation.
@@ -511,10 +600,70 @@ get([1, 2, 3], 1) == 2
511600
get({"name": "John", "age": 30}, "name") == "John"
512601
```
513602

603+
### take(array, n)
604+
605+
Returns the first `n` elements from an array. If the array has fewer than `n` elements, returns the whole array.
606+
607+
```expr
608+
take([1, 2, 3, 4], 2) == [1, 2]
609+
```
610+
611+
### keys(map)
612+
613+
Returns an array containing the keys of the map.
614+
615+
```expr
616+
keys({"name": "John", "age": 30}) == ["name", "age"]
617+
```
618+
619+
### values(map)
620+
621+
Returns an array containing the values of the map.
622+
623+
```expr
624+
values({"name": "John", "age": 30}) == ["John", 30]
625+
```
626+
627+
### toPairs(map)
628+
629+
Converts a map to an array of key-value pairs.
630+
631+
```expr
632+
toPairs({"name": "John", "age": 30}) == [["name", "John"], ["age", 30]]
633+
```
634+
635+
### fromPairs(array)
636+
637+
Converts an array of key-value pairs to a map.
638+
639+
```expr
640+
fromPairs([["name", "John"], ["age", 30]]) == {"name": "John", "age": 30}
641+
```
642+
643+
### sort(array[, order])
644+
645+
Sorts an array in ascending order. Optional `order` argument can be used to specify the order of sorting: `asc`
646+
or `desc`.
647+
648+
```expr
649+
sort([3, 1, 4]) == [1, 3, 4]
650+
sort([3, 1, 4], "desc") == [4, 3, 1]
651+
```
652+
653+
### sortBy(array, key[, order])
654+
655+
Sorts an array of maps by a specific key in ascending order. Optional `order` argument can be used to specify the order
656+
of sorting: `asc` or `desc`.
657+
658+
```expr
659+
sortBy(users, "Age")
660+
sortBy(users, "Age", "desc")
661+
```
662+
514663
## Predicate
515664

516-
The predicate is an expression that accepts a single argument. To access
517-
the argument use the `#` symbol.
665+
The predicate is an expression. It takes one or more arguments and returns a boolean value.
666+
To access the arguments, the `#` symbol is used.
518667

519668
```expr
520669
map(0..9, {# / 2})

0 commit comments

Comments
 (0)