Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ Besides the "operator" functions, there are several pre-defined functions. You c
| Function | Description |
|:------------- |:----------- |
| count(a) | Returns the number of items in an array. |
| map(f, a) | Array map: Pass each element of `a` the function `f`, and return an array of the results. |
| fold(f, y, a) | Array fold: Fold/reduce array `a` into a single value, `y` by setting `y = f(y, x, index)` for each element `x` of the array. |
| reduce(f, y, a) | Alias for `fold`. Reduces array `a` into a single value using function `f` starting with accumulator `y`. |
| filter(f, a) | Array filter: Return an array containing only the values from `a` where `f(x, index)` is `true`. |
| find(f, a) | Returns the first element in array `a` where `f(x, index)` is `true`, or `undefined` if not found. |
| some(f, a) | Returns `true` if at least one element in array `a` satisfies `f(x, index)`, `false` otherwise. |
| every(f, a) | Returns `true` if all elements in array `a` satisfy `f(x, index)`. Returns `true` for empty arrays. |
| map(a, f) | Array map: Pass each element of `a` to the function `f`, and return an array of the results. |
| fold(a, y, f) | Array fold: Fold/reduce array `a` into a single value, `y` by setting `y = f(y, x, index)` for each element `x` of the array. |
| reduce(a, y, f) | Alias for `fold`. Reduces array `a` into a single value using function `f` starting with accumulator `y`. |
| filter(a, f) | Array filter: Return an array containing only the values from `a` where `f(x, index)` is `true`. |
| find(a, f) | Returns the first element in array `a` where `f(x, index)` is `true`, or `undefined` if not found. |
| some(a, f) | Returns `true` if at least one element in array `a` satisfies `f(x, index)`, `false` otherwise. |
| every(a, f) | Returns `true` if all elements in array `a` satisfy `f(x, index)`. Returns `true` for empty arrays. |
| unique(a) | Returns a new array with duplicate values removed from array `a`. |
| distinct(a) | Alias for `unique`. Returns a new array with duplicate values removed. |
| indexOf(x, a) | Return the first index of string or array `a` matching the value `x`, or `-1` if not found. |
Expand Down Expand Up @@ -359,14 +359,14 @@ factorial(x) = x < 2 ? 1 : x * factorial(x - 1)
These functions can than be used in other functions that require a function argument, such as `map`, `filter` or `fold`:
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "can than be" should be "can then be".

Suggested change
These functions can than be used in other functions that require a function argument, such as `map`, `filter` or `fold`:
These functions can then be used in other functions that require a function argument, such as `map`, `filter` or `fold`:

Copilot uses AI. Check for mistakes.

```js
name(u) = u.name; map(name, users)
add(a, b) = a+b; fold(add, 0, [1, 2, 3])
name(u) = u.name; map(users, name)
add(a, b) = a+b; fold([1, 2, 3], 0, add)
```

You can also define the functions inline:

```js
filter(isEven(x) = x % 2 == 0, [1, 2, 3, 4, 5])
filter([1, 2, 3, 4, 5], isEven(x) = x % 2 == 0)
```

### Arrow Functions
Expand All @@ -376,18 +376,18 @@ Arrow functions provide a concise syntax for inline functions, similar to JavaSc
**Single parameter (no parentheses required):**

```js
map(x => x * 2, [1, 2, 3]) // [2, 4, 6]
filter(x => x > 2, [1, 2, 3, 4]) // [3, 4]
map(x => x.name, users) // Extract property from objects
map([1, 2, 3], x => x * 2) // [2, 4, 6]
filter([1, 2, 3, 4], x => x > 2) // [3, 4]
map(users, x => x.name) // Extract property from objects
```

**Multiple parameters (parentheses required):**

```js
fold((acc, x) => acc + x, 0, [1, 2, 3, 4, 5]) // 15 (sum)
fold((acc, x) => acc * x, 1, [1, 2, 3, 4, 5]) // 120 (product)
map((val, idx) => val + idx, [10, 20, 30]) // [10, 21, 32]
filter((x, i) => i >= 1, [10, 20, 30]) // [20, 30]
fold([1, 2, 3, 4, 5], 0, (acc, x) => acc + x) // 15 (sum)
fold([1, 2, 3, 4, 5], 1, (acc, x) => acc * x) // 120 (product)
map([10, 20, 30], (val, idx) => val + idx) // [10, 21, 32]
filter([10, 20, 30], (x, i) => i >= 1) // [20, 30]
```

**Zero parameters:**
Expand All @@ -401,23 +401,23 @@ filter((x, i) => i >= 1, [10, 20, 30]) // [20, 30]
Arrow functions can be assigned to variables for reuse:

```js
fn = x => x * 2; map(fn, [1, 2, 3]) // [2, 4, 6]
double = x => x * 2; triple = x => x * 3; map(double, map(triple, [1, 2])) // [6, 12]
fn = x => x * 2; map([1, 2, 3], fn) // [2, 4, 6]
double = x => x * 2; triple = x => x * 3; map(map([1, 2], triple), double) // [6, 12]
```

**Nested arrow functions:**

```js
map(row => map(x => x * 2, row), [[1, 2], [3, 4]]) // [[2, 4], [6, 8]]
map([[1, 2], [3, 4]], row => map(row, x => x * 2)) // [[2, 4], [6, 8]]
```

**With member access and complex expressions:**

```js
filter(x => x.age > 25, users) // Filter objects by property
map(x => x.value * 2 + 1, items) // Complex transformations
filter(x => x > 0 and x < 10, numbers) // Using logical operators
map(x => x > 5 ? "high" : "low", [3, 7, 2, 9]) // Using ternary operator
filter(users, x => x.age > 25) // Filter objects by property
map(items, x => x.value * 2 + 1) // Complex transformations
filter(numbers, x => x > 0 and x < 10) // Using logical operators
map([3, 7, 2, 9], x => x > 5 ? "high" : "low") // Using ternary operator
```

> **Note:** Arrow functions share the same `fndef` operator flag as traditional function definitions. If function definitions are disabled via parser options, arrow functions will also be disabled.
Expand All @@ -429,25 +429,25 @@ The new array utility functions provide additional ways to work with arrays:
**Using reduce (alias for fold):**

```js
reduce((acc, x) => acc + x, 0, [1, 2, 3, 4]) // 10 (sum using reduce)
reduce((acc, x) => acc * x, 1, [2, 3, 4]) // 24 (product)
reduce([1, 2, 3, 4], 0, (acc, x) => acc + x) // 10 (sum using reduce)
reduce([2, 3, 4], 1, (acc, x) => acc * x) // 24 (product)
```

**Using find:**

```js
find(x => x > 5, [1, 3, 7, 2, 9]) // 7 (first element > 5)
find(x => x < 0, [1, 2, 3]) // undefined (not found)
find(x => x.age > 18, users) // First user over 18
find([1, 3, 7, 2, 9], x => x > 5) // 7 (first element > 5)
find([1, 2, 3], x => x < 0) // undefined (not found)
find(users, x => x.age > 18) // First user over 18
```

**Using some and every:**

```js
some(x => x > 10, [1, 5, 15, 3]) // true (at least one > 10)
every(x => x > 0, [1, 2, 3, 4]) // true (all positive)
every(x => x % 2 == 0, [2, 4, 5, 6]) // false (not all even)
some(x => x < 0, [1, 2, 3]) // false (none negative)
some([1, 5, 15, 3], x => x > 10) // true (at least one > 10)
every([1, 2, 3, 4], x => x > 0) // true (all positive)
every([2, 4, 5, 6], x => x % 2 == 0) // false (not all even)
some([1, 2, 3], x => x < 0) // false (none negative)
```

**Using unique/distinct:**
Expand All @@ -462,11 +462,11 @@ unique([]) // []

```js
// Filter positive numbers, remove duplicates, then double each
unique(filter(x => x > 0, [1, -2, 3, 3, -4, 5, 1])) // [1, 3, 5]
map(x => x * 2, unique([1, 2, 2, 3])) // [2, 4, 6]
unique(filter([1, -2, 3, 3, -4, 5, 1], x => x > 0)) // [1, 3, 5]
map(unique([1, 2, 2, 3]), x => x * 2) // [2, 4, 6]

// Find first even number greater than 5
find(x => x % 2 == 0, filter(x => x > 5, [3, 7, 8, 9, 10])) // 8
find(filter([3, 7, 8, 9, 10], x => x > 5), x => x % 2 == 0) // 8
```

### Examples of Type Checking Functions
Expand Down Expand Up @@ -497,29 +497,29 @@ if(isString(x), toUpper(x), x) // Uppercase if string
**Using with filter:**

```js
filter(isNumber, [1, "a", 2, "b", 3]) // [1, 2, 3]
filter(isString, [1, "a", 2, "b", 3]) // ["a", "b"]
filter([1, "a", 2, "b", 3], isNumber) // [1, 2, 3]
filter([1, "a", 2, "b", 3], isString) // ["a", "b"]
```

**Using with some/every:**

```js
some(isString, [1, 2, "hello", 3]) // true (has at least one string)
every(isNumber, [1, 2, 3, 4]) // true (all are numbers)
every(isNumber, [1, "a", 3]) // false (not all numbers)
some([1, 2, "hello", 3], isString) // true (has at least one string)
every([1, 2, 3, 4], isNumber) // true (all are numbers)
every([1, "a", 3], isNumber) // false (not all numbers)
```

**Practical examples:**

```js
// Count how many strings are in an array
count(filter(isString, [1, "a", 2, "b", 3])) // 2
count(filter([1, "a", 2, "b", 3], isString)) // 2

// Get the first number in a mixed array
find(isNumber, ["a", "b", 3, "c", 5]) // 3
find(["a", "b", 3, "c", 5], isNumber) // 3

// Check if any value is null or undefined
some(x => isNull(x) or isUndefined(x), data) // true/false
some(data, x => isNull(x) or isUndefined(x)) // true/false
```

## Custom JavaScript Functions
Expand Down
Loading
Loading