# Scripting (Beta)

| **Category**    | **Operator**        | **Description**                               | **IronPython Lambda Example**                                            |
| --------------- | ------------------- | --------------------------------------------- | ------------------------------------------------------------------------ |
| **Filtering**   | `Where`             | Filters elements based on a predicate         | `list.Where(lambda x: x > 5)`                                            |
| **Projection**  | `Select`            | Projects each element into a new form         | `list.Select(lambda x: x * 2)`                                           |
|                 | `SelectMany`        | Flattens collections into one sequence        | `list.SelectMany(lambda x: x)`                                           |
| **Sorting**     | `OrderBy`           | Sorts ascending by key                        | `list.OrderBy(lambda x: x.Age)`                                          |
|                 | `OrderByDescending` | Sorts descending by key                       | `list.OrderByDescending(lambda x: x.Age)`                                |
| **Grouping**    | `GroupBy`           | Groups by key                                 | `list.GroupBy(lambda x: x.Type)`                                         |
| **Joining**     | `Join`              | Joins two sequences based on matching keys    | `outer.Join(inner, lambda o: o.ID, lambda i: i.ID, lambda o, i: (o, i))` |
| **Aggregation** | `Count`             | Counts elements                               | `list.Count()`                                                           |
|                 | `Sum`               | Sums values                                   | `list.Sum(lambda x: x.Price)`                                            |
|                 | `Average`           | Averages values                               | `list.Average(lambda x: x.Score)`                                        |
|                 | `Min`               | Gets minimum value                            | `list.Min(lambda x: x.Age)`                                              |
|                 | `Max`               | Gets maximum value                            | `list.Max(lambda x: x.Age)`                                              |
| **Quantifiers** | `Any`               | Checks if any element satisfies the condition | `list.Any(lambda x: x.IsActive)`                                         |
|                 | `All`               | Checks if all elements satisfy the condition  | `list.All(lambda x: x.IsValid)`                                          |
| **Element**     | `First`             | Gets first element                            | `list.First()`                                                           |
|                 | `FirstOrDefault`    | Gets first or default                         | `list.FirstOrDefault()`                                                  |
|                 | `Single`            | Expects only one element                      | `list.Single(lambda x: x.ID == 3)`                                       |
|                 | `ElementAt`         | Gets element at specific index                | `list.ElementAt(2)`                                                      |
| **Set**         | `Distinct`          | Removes duplicates                            | `list.Distinct()`                                                        |
|                 | `Union`             | Combines sequences and removes duplicates     | `list1.Union(list2)`                                                     |
|                 | `Intersect`         | Gets common elements                          | `list1.Intersect(list2)`                                                 |
|                 | `Except`            | Gets elements not in the second sequence      | `list1.Except(list2)`                                                    |
| **Conversion**  | `ToList`            | Converts to list                              | `list.ToList()`                                                          |
|                 | `ToDictionary`      | Converts to dictionary                        | `list.ToDictionary(lambda x: x.ID)`                                      |
| **Generation**  | `Range`             | Generates range of integers                   | `Enumerable.Range(1, 10)`                                                |
|                 | `Repeat`            | Repeats a value                               | `Enumerable.Repeat("A", 5)`                                              |

| **Function**       | **Description**                                              | **Example**                     |
| ------------------ | ------------------------------------------------------------ | ------------------------------- |
| `Math.Abs(x)`      | Returns the absolute value of `x`                            | `Math.Abs(-5)` → `5`            |
| `Math.Ceiling(x)`  | Rounds up to the smallest integer ≥ `x`                      | `Math.Ceiling(2.3)` → `3.0`     |
| `Math.Floor(x)`    | Rounds down to the largest integer ≤ `x`                     | `Math.Floor(2.9)` → `2.0`       |
| `Math.Round(x)`    | Rounds to the nearest integer (banker's rounding by default) | `Math.Round(2.5)` → `2.0`       |
| `Math.Truncate(x)` | Removes the decimal portion (rounds toward zero)             | `Math.Truncate(3.9)` → `3.0`    |
| `Math.Pow(x, y)`   | Returns `x` raised to the power `y`                          | `Math.Pow(2, 3)` → `8.0`        |
| `Math.Sqrt(x)`     | Returns the square root of `x`                               | `Math.Sqrt(16)` → `4.0`         |
| `Math.Log(x)`      | Returns the natural logarithm (base e) of `x`                | `Math.Log(10)` → `~2.302`       |
| `Math.Log10(x)`    | Returns the base-10 logarithm of `x`                         | `Math.Log10(100)` → `2.0`       |
| `Math.Exp(x)`      | Returns e raised to the power of `x`                         | `Math.Exp(1)` → `~2.718`        |
| `Math.Sin(x)`      | Returns sine of angle `x` (radians)                          | `Math.Sin(Math.PI / 2)` → `1.0` |
| `Math.Cos(x)`      | Returns cosine of angle `x` (radians)                        | `Math.Cos(0)` → `1.0`           |
| `Math.Tan(x)`      | Returns tangent of angle `x` (radians)                       | `Math.Tan(0)` → `0.0`           |
| `Math.Asin(x)`     | Returns angle whose sine is `x`                              | `Math.Asin(1)` → `π/2`          |
| `Math.Acos(x)`     | Returns angle whose cosine is `x`                            | `Math.Acos(1)` → `0`            |
| `Math.Atan(x)`     | Returns angle whose tangent is `x`                           | `Math.Atan(1)` → `π/4`          |
| `Math.Atan2(y, x)` | Returns angle from X-axis to point (x, y)                    | `Math.Atan2(1, 1)` → `π/4`      |
| `Math.Max(x, y)`   | Returns the greater of `x` and `y`                           | `Math.Max(4, 9)` → `9`          |
| `Math.Min(x, y)`   | Returns the smaller of `x` and `y`                           | `Math.Min(4, 9)` → `4`          |
| `Math.Sign(x)`     | Returns `-1`, `0`, or `1` depending on the sign of `x`       | `Math.Sign(-12)` → `-1`         |

Projection

SelectMany

```
<style>
#preExclude#
td {
    border: 1px solid black;
    border-spacing: 0;
}

table {
    border-collapse: collapse;
}

.amount {
    text-align: right;
}

.total {
    font-weight: bold;
}
#end-preExclude#
</style>

{
paymentList = entries.SelectMany(lambda x: x["Payment"], lambda x,y: dict(Payment = y, Document = x)).GroupBy(lambda x: x['Payment']['Date'])
}

<h3>Payment List</h3>

<table style="width: 100%; border: 1px solid black;">
    <tr>
        <td>Payment Date</td>
        <td>Amount</td>
        <td>Code</td>
    </tr>
    {start-repeat:item in paymentList}
    <tr>
        <td><b>{item[0]['Payment']['Date']|dd-MMM-yyyy}</b></td>
        <td class="amount"><b>{sum(x['Payment']['Payment Amount'] for x in item)|n2}</b></td>
        <td><b>{item[0]['Payment']['Code']}</b></td>
    </tr>
    {start-repeat:detail in item}
    <tr>
        <td>{detail['Document'].DocumentId}</td>
        <td class="amount">{detail['Document']['Grand Total']|n2}</td>
        <td class="amount">{detail['Payment']['Payment Amount']|n2}</td>
        <td class="amount">{detail['Document']['Grand Total'] - detail['Payment']['Payment Amount']|n2}</td>
    </tr>
    {end-repeat}
    {end-repeat}
    <tr>
        <td></td>
    </tr>
</table>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://inistate.gitbook.io/home/advanced/scripting-beta.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
