github.com/expr-lang/expr@v1.16.9/docs/language-definition.md (about)

     1  # Language Definition
     2  
     3  **Expr** is a simple expression language that can be used to evaluate expressions.
     4  
     5  ## Literals
     6  
     7  <table>
     8      <tr>
     9          <td><strong>Comment</strong></td>
    10          <td>
    11               <code>/* */</code> or <code>//</code>
    12          </td>
    13      </tr>
    14      <tr>
    15          <td><strong>Boolean</strong></td>
    16          <td>
    17              <code>true</code>, <code>false</code>
    18          </td>
    19      </tr>
    20      <tr>
    21          <td><strong>Integer</strong></td>
    22          <td>
    23              <code>42</code>, <code>0x2A</code>, <code>0o52</code>, <code>0b101010</code>
    24          </td>
    25      </tr>
    26      <tr>
    27          <td><strong>Float</strong></td>
    28          <td>
    29              <code>0.5</code>, <code>.5</code>
    30          </td>
    31      </tr>
    32      <tr>
    33          <td><strong>String</strong></td>
    34          <td>
    35              <code>"foo"</code>, <code>'bar'</code>
    36          </td>
    37      </tr>
    38      <tr>
    39          <td><strong>Array</strong></td>
    40          <td>
    41              <code>[1, 2, 3]</code>
    42          </td>
    43      </tr>
    44      <tr>
    45          <td><strong>Map</strong></td>
    46          <td>
    47              <code>&#123;a: 1, b: 2, c: 3&#125;</code>
    48          </td>
    49      </tr>
    50      <tr>
    51          <td><strong>Nil</strong></td>
    52          <td>
    53              <code>nil</code>
    54          </td>
    55      </tr>
    56  </table>
    57  
    58  ### Strings
    59  
    60  Strings can be enclosed in single quotes or double quotes. Strings can contain escape sequences, like `\n` for newline,
    61  `\t` for tab, `\uXXXX` for Unicode code points.
    62  
    63  ```expr
    64  "Hello\nWorld"
    65  ```
    66  
    67  For multiline strings, use backticks:
    68  
    69  ```expr
    70  `Hello
    71  World`
    72  ```
    73  
    74  Backticks strings are raw strings, they do not support escape sequences.
    75  
    76  ## Operators
    77  
    78  <table>
    79      <tr>
    80          <td><strong>Arithmetic</strong></td>
    81          <td>
    82              <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code> (modulus), <code>^</code> or <code>**</code> (exponent)
    83          </td>
    84      </tr>
    85      <tr>
    86          <td><strong>Comparison</strong></td>
    87          <td>
    88              <code>==</code>, <code>!=</code>, <code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code>, <code>&gt;=</code>
    89          </td>
    90      </tr>
    91      <tr>
    92          <td><strong>Logical</strong></td>
    93          <td>
    94              <code>not</code> or <code>!</code>, <code>and</code> or <code>&amp;&amp;</code>, <code>or</code> or <code>||</code>
    95          </td>
    96      </tr>
    97      <tr>
    98          <td><strong>Conditional</strong></td>
    99          <td>
   100              <code>?:</code> (ternary), <code>??</code> (nil coalescing)
   101          </td>
   102      </tr>
   103      <tr>
   104          <td><strong>Membership</strong></td>
   105          <td>
   106              <code>[]</code>, <code>.</code>, <code>?.</code>, <code>in</code>
   107          </td>
   108      </tr>
   109      <tr>
   110          <td><strong>String</strong></td>
   111          <td>
   112              <code>+</code> (concatenation), <code>contains</code>, <code>startsWith</code>, <code>endsWith</code>
   113          </td>
   114      </tr>
   115      <tr>
   116          <td><strong>Regex</strong></td>
   117          <td>
   118              <code>matches</code>
   119          </td>
   120      </tr>
   121      <tr>
   122          <td><strong>Range</strong></td>
   123          <td>
   124              <code>..</code>
   125          </td>
   126      </tr>
   127      <tr>
   128          <td><strong>Slice</strong></td>
   129          <td>
   130              <code>[:]</code>
   131          </td>
   132      </tr>
   133      <tr>
   134          <td><strong>Pipe</strong></td>
   135          <td>
   136              <code>|</code>
   137          </td>
   138      </tr>
   139  </table>
   140  
   141  ### Membership Operator
   142  
   143  Fields of structs and items of maps can be accessed with `.` operator
   144  or `[]` operator. Next two expressions are equivalent:
   145  
   146  ```expr
   147  user.Name
   148  user["Name"]
   149  ``` 
   150  
   151  Elements of arrays and slices can be accessed with
   152  `[]` operator. Negative indices are supported with `-1` being
   153  the last element.
   154  
   155  ```expr
   156  array[0] // first element
   157  array[-1] // last element
   158  ```
   159  
   160  The `in` operator can be used to check if an item is in an array or a map.
   161  
   162  ```expr
   163  "John" in ["John", "Jane"]
   164  "name" in {"name": "John", "age": 30}
   165  ```
   166  
   167  #### Optional chaining
   168  
   169  The `?.` operator can be used to access a field of a struct or an item of a map
   170  without checking if the struct or the map is `nil`. If the struct or the map is
   171  `nil`, the result of the expression is `nil`.
   172  
   173  ```expr
   174  author.User?.Name
   175  ```
   176  
   177  Is equivalent to:
   178  
   179  ```expr
   180  author.User != nil ? author.User.Name : nil
   181  ```
   182  
   183  #### Nil coalescing
   184  
   185  The `??` operator can be used to return the left-hand side if it is not `nil`,
   186  otherwise the right-hand side is returned.
   187  
   188  ```expr
   189  author.User?.Name ?? "Anonymous"
   190  ```
   191  
   192  Is equivalent to:
   193  
   194  ```expr
   195  author.User != nil ? author.User.Name : "Anonymous"
   196  ```
   197  
   198  ### Slice Operator
   199  
   200  The slice operator `[:]` can be used to access a slice of an array.
   201  
   202  For example, variable **array** is `[1, 2, 3, 4, 5]`:
   203  
   204  ```expr
   205  array[1:4] == [2, 3, 4]
   206  array[1:-1] == [2, 3, 4]
   207  array[:3] == [1, 2, 3]
   208  array[3:] == [4, 5]
   209  array[:] == array
   210  ```
   211  
   212  ### Pipe Operator
   213  
   214  The pipe operator `|` can be used to pass the result of the left-hand side
   215  expression as the first argument of the right-hand side expression.
   216  
   217  ```expr
   218  user.Name | lower() | split(" ")
   219  ```
   220  
   221  Is equivalent to:
   222  
   223  ```expr
   224  split(lower(user.Name), " ")
   225  ```
   226  
   227  ### Range Operator
   228  
   229  The range operator `..` can be used to create a range of integers.
   230  
   231  ```expr
   232  1..3 == [1, 2, 3]
   233  ```
   234  
   235  ## Variables
   236  
   237  Variables can be declared with the `let` keyword. The variable name must start with a letter or an underscore.
   238  The variable name can contain letters, digits and underscores. After the variable is declared, it can be used in the
   239  expression.
   240  
   241  ```expr
   242  let x = 42; x * 2
   243  ```
   244  
   245  A few variables can be declared by a few `let` statements separated by a semicolon.
   246  
   247  ```expr
   248  let x = 42; 
   249  let y = 2; 
   250  x * y
   251  ```
   252  
   253  Here is an example of variable with pipe operator:
   254  
   255  ```expr
   256  let name = user.Name | lower() | split(" "); 
   257  "Hello, " + name[0] + "!"
   258  ```
   259  
   260  ### $env
   261  
   262  The `$env` variable is a map of all variables passed to the expression.
   263  
   264  ```expr
   265  foo.Name == $env["foo"].Name
   266  $env["var with spaces"]
   267  ```
   268  
   269  Think of `$env` as a global variable that contains all variables.
   270  
   271  The `$env` can be used to check if a variable is defined:
   272  
   273  ```expr
   274  'foo' in $env
   275  ```
   276  
   277  ## Predicate
   278  
   279  The predicate is an expression. Predicates can be used in functions like `filter`, `all`, `any`, `one`, `none`, etc.
   280  For example, next expression creates a new array from 0 to 9 and then filters it by even numbers:
   281  
   282  ```expr
   283  filter(0..9, {# % 2 == 0})
   284  ```
   285  
   286  If items of the array is a struct or a map, it is possible to access fields with
   287  omitted `#` symbol (`#.Value` becomes `.Value`).
   288  
   289  ```expr
   290  filter(tweets, {len(.Content) > 240})
   291  ```
   292  
   293  Braces `{` `}` can be omitted:
   294  
   295  ```expr
   296  filter(tweets, len(.Content) > 240)
   297  ```
   298  
   299  :::tip
   300  In nested predicates, to access the outer variable, use [variables](#variables).
   301  
   302  ```expr
   303  filter(posts, {
   304      let post = #; 
   305      any(.Comments, .Author == post.Author)
   306  }) 
   307  ```
   308  
   309  :::
   310  
   311  ## String Functions
   312  
   313  ### trim(str[, chars]) {#trim}
   314  
   315  Removes white spaces from both ends of a string `str`.
   316  If the optional `chars` argument is given, it is a string specifying the set of characters to be removed.
   317  
   318  ```expr
   319  trim("  Hello  ") == "Hello"
   320  trim("__Hello__", "_") == "Hello"
   321  ```
   322  
   323  ### trimPrefix(str, prefix) {#trimPrefix}
   324  
   325  Removes the specified prefix from the string `str` if it starts with that prefix.
   326  
   327  ```expr
   328  trimPrefix("HelloWorld", "Hello") == "World"
   329  ```
   330  
   331  ### trimSuffix(str, suffix) {#trimSuffix}
   332  
   333  Removes the specified suffix from the string `str` if it ends with that suffix.
   334  
   335  ```expr
   336  trimSuffix("HelloWorld", "World") == "Hello"
   337  ```
   338  
   339  ### upper(str) {#upper}
   340  
   341  Converts all the characters in string `str` to uppercase.
   342  
   343  ```expr
   344  upper("hello") == "HELLO"
   345  ```
   346  
   347  ### lower(str) {#lower}
   348  
   349  Converts all the characters in string `str` to lowercase.
   350  
   351  ```expr
   352  lower("HELLO") == "hello"
   353  ```
   354  
   355  ### split(str, delimiter[, n]) {#split}
   356  
   357  Splits the string `str` at each instance of the delimiter and returns an array of substrings.
   358  
   359  ```expr
   360  split("apple,orange,grape", ",") == ["apple", "orange", "grape"]
   361  split("apple,orange,grape", ",", 2) == ["apple", "orange,grape"]
   362  ```
   363  
   364  ### splitAfter(str, delimiter[, n]) {#splitAfter}
   365  
   366  Splits the string `str` after each instance of the delimiter.
   367  
   368  ```expr
   369  splitAfter("apple,orange,grape", ",") == ["apple,", "orange,", "grape"]
   370  splitAfter("apple,orange,grape", ",", 2) == ["apple,", "orange,grape"]
   371  ```
   372  
   373  ### replace(str, old, new) {#replace}
   374  
   375  Replaces all occurrences of `old` in string `str` with `new`.
   376  
   377  ```expr
   378  replace("Hello World", "World", "Universe") == "Hello Universe"
   379  ```
   380  
   381  ### repeat(str, n) {#repeat}
   382  
   383  Repeats the string `str` `n` times.
   384  
   385  ```expr
   386  repeat("Hi", 3) == "HiHiHi"
   387  ```
   388  
   389  ### indexOf(str, substring) {#indexOf}
   390  
   391  Returns the index of the first occurrence of the substring in string `str` or -1 if not found.
   392  
   393  ```expr
   394  indexOf("apple pie", "pie") == 6
   395  ```
   396  
   397  ### lastIndexOf(str, substring) {#lastIndexOf}
   398  
   399  Returns the index of the last occurrence of the substring in string `str` or -1 if not found.
   400  
   401  ```expr
   402  lastIndexOf("apple pie apple", "apple") == 10
   403  ```
   404  
   405  ### hasPrefix(str, prefix) {#hasPrefix}
   406  
   407  Returns `true` if string `str` starts with the given prefix.
   408  
   409  ```expr
   410  hasPrefix("HelloWorld", "Hello") == true
   411  ```
   412  
   413  ### hasSuffix(str, suffix) {#hasSuffix}
   414  
   415  Returns `true` if string `str` ends with the given suffix.
   416  
   417  ```expr
   418  hasSuffix("HelloWorld", "World") == true
   419  ```
   420  
   421  ## Date Functions
   422  
   423  Expr has a built-in support for Go's [time package](https://pkg.go.dev/time).
   424  It is possible to subtract two dates and get the duration between them:
   425  
   426  ```expr
   427  createdAt - now()
   428  ```
   429  
   430  It is possible to add a duration to a date:
   431  
   432  ```expr
   433  createdAt + duration("1h")
   434  ```
   435  
   436  And it is possible to compare dates:
   437  
   438  ```expr
   439  createdAt > now() - duration("1h")
   440  ```
   441  
   442  ### now() {#now}
   443  
   444  Returns the current date as a [time.Time](https://pkg.go.dev/time#Time) value.
   445  
   446  ```expr
   447  now().Year() == 2024
   448  ```
   449  
   450  ### duration(str) {#duration}
   451  
   452  Returns [time.Duration](https://pkg.go.dev/time#Duration) value of the given string `str`.
   453  
   454  Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
   455  
   456  ```expr
   457  duration("1h").Seconds() == 3600
   458  ```
   459  
   460  ### date(str[, format[, timezone]]) {#date}
   461  
   462  Converts the given string `str` into a date representation.
   463  
   464  If the optional `format` argument is given, it is a string specifying the format of the date.
   465  The format string uses the same formatting rules as the standard
   466  Go [time package](https://pkg.go.dev/time#pkg-constants).
   467  
   468  If the optional `timezone` argument is given, it is a string specifying the timezone of the date.
   469  
   470  If the `format` argument is not given, the `v` argument must be in one of the following formats:
   471  
   472  - 2006-01-02
   473  - 15:04:05
   474  - 2006-01-02 15:04:05
   475  - RFC3339
   476  - RFC822,
   477  - RFC850,
   478  - RFC1123,
   479  
   480  ```expr
   481  date("2023-08-14")
   482  date("15:04:05")
   483  date("2023-08-14T00:00:00Z")
   484  date("2023-08-14 00:00:00", "2006-01-02 15:04:05", "Europe/Zurich")
   485  ```
   486  
   487  Available methods on the date:
   488  
   489  - `Year()` - returns the year 
   490  - `Month()` - returns the month (starting from 1)
   491  - `Day()` - returns the day of the month
   492  - `Hour()` - returns the hour
   493  - `Minute()` - returns the minute
   494  - `Second()` - returns the second
   495  - `Weekday()` - returns the day of the week
   496  - `YearDay()` - returns the day of the year
   497  - and [more](https://pkg.go.dev/time#Time).
   498  
   499  ```expr
   500  date("2023-08-14").Year() == 2023
   501  ```
   502  
   503  ### timezone(str) {#timezone}
   504  
   505  Returns the timezone of the given string `str`. List of available timezones can be
   506  found [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
   507  
   508  ```expr
   509  timezone("Europe/Zurich")
   510  timezone("UTC")
   511  ```
   512  
   513  To convert a date to a different timezone, use the [`In()`](https://pkg.go.dev/time#Time.In) method:
   514  
   515  ```expr
   516  date("2023-08-14 00:00:00").In(timezone("Europe/Zurich"))
   517  ```
   518  
   519  ## Number Functions
   520  
   521  ### max(n1, n2) {#max}
   522  
   523  Returns the maximum of the two numbers `n1` and `n2`.
   524  
   525  ```expr
   526  max(5, 7) == 7
   527  ```
   528  
   529  ### min(n1, n2) {#min}
   530  
   531  Returns the minimum of the two numbers `n1` and `n2`.
   532  
   533  ```expr
   534  min(5, 7) == 5
   535  ```
   536  
   537  ### abs(n) {#abs}
   538  
   539  Returns the absolute value of a number.
   540  
   541  ```expr
   542  abs(-5) == 5
   543  ```
   544  
   545  ### ceil(n) {#ceil}
   546  
   547  Returns the least integer value greater than or equal to x.
   548  
   549  ```expr
   550  ceil(1.5) == 2.0
   551  ```
   552  
   553  ### floor(n) {#floor}
   554  
   555  Returns the greatest integer value less than or equal to x.
   556  
   557  ```expr
   558  floor(1.5) == 1.0
   559  ```
   560  
   561  ### round(n) {#round}
   562  
   563  Returns the nearest integer, rounding half away from zero.
   564  
   565  ```expr
   566  round(1.5) == 2.0
   567  ```
   568  
   569  ## Array Functions
   570  
   571  ### all(array, predicate) {#all}
   572  
   573  Returns **true** if all elements satisfies the [predicate](#predicate).
   574  If the array is empty, returns **true**.
   575  
   576  ```expr
   577  all(tweets, {.Size < 280})
   578  ```
   579  
   580  ### any(array, predicate) {#any}
   581  
   582  Returns **true** if any elements satisfies the [predicate](#predicate).
   583  If the array is empty, returns **false**.
   584  
   585  ```expr
   586  any(tweets, {.Size > 280})
   587  ```
   588  
   589  ### one(array, predicate) {#one}
   590  
   591  Returns **true** if _exactly one_ element satisfies the [predicate](#predicate).
   592  If the array is empty, returns **false**.
   593  
   594  ```expr
   595  one(participants, {.Winner})
   596  ```
   597  
   598  ### none(array, predicate) {#none}
   599  
   600  Returns **true** if _all elements does not_ satisfy the [predicate](#predicate).
   601  If the array is empty, returns **true**.
   602  
   603  ```expr
   604  none(tweets, {.Size > 280})
   605  ```
   606  
   607  ### map(array, predicate) {#map}
   608  
   609  Returns new array by applying the [predicate](#predicate) to each element of
   610  the array.
   611  
   612  ```expr
   613  map(tweets, {.Size})
   614  ```
   615  
   616  ### filter(array, predicate) {#filter}
   617  
   618  Returns new array by filtering elements of the array by [predicate](#predicate).
   619  
   620  ```expr
   621  filter(users, .Name startsWith "J")
   622  ```
   623  
   624  ### find(array, predicate) {#find}
   625  
   626  Finds the first element in an array that satisfies the [predicate](#predicate).
   627  
   628  ```expr
   629  find([1, 2, 3, 4], # > 2) == 3
   630  ```
   631  
   632  ### findIndex(array, predicate) {#findIndex}
   633  
   634  Finds the index of the first element in an array that satisfies the [predicate](#predicate).
   635  
   636  ```expr
   637  findIndex([1, 2, 3, 4], # > 2) == 2
   638  ```
   639  
   640  ### findLast(array, predicate) {#findLast}
   641  
   642  Finds the last element in an array that satisfies the [predicate](#predicate).
   643  
   644  ```expr
   645  findLast([1, 2, 3, 4], # > 2) == 4
   646  ```
   647  
   648  ### findLastIndex(array, predicate) {#findLastIndex}
   649  
   650  Finds the index of the last element in an array that satisfies the [predicate](#predicate).
   651  
   652  ```expr
   653  findLastIndex([1, 2, 3, 4], # > 2) == 3
   654  ```
   655  
   656  ### groupBy(array, predicate) {#groupBy}
   657  
   658  Groups the elements of an array by the result of the [predicate](#predicate).
   659  
   660  ```expr
   661  groupBy(users, .Age)
   662  ```
   663  
   664  ### count(array[, predicate]) {#count}
   665  
   666  Returns the number of elements what satisfies the [predicate](#predicate).
   667  
   668  ```expr
   669  count(users, .Age > 18)
   670  ```
   671  
   672  Equivalent to:
   673  
   674  ```expr
   675  len(filter(users, .Age > 18))
   676  ```
   677  
   678  If the predicate is not given, returns the number of `true` elements in the array.
   679  
   680  ```expr
   681  count([true, false, true]) == 2
   682  ```
   683  
   684  ### concat(array1, array2[, ...]) {#concat}
   685  
   686  Concatenates two or more arrays.
   687  
   688  ```expr
   689  concat([1, 2], [3, 4]) == [1, 2, 3, 4]
   690  ```
   691  
   692  ### join(array[, delimiter]) {#join}
   693  
   694  Joins an array of strings into a single string with the given delimiter.
   695  If no delimiter is given, an empty string is used.
   696  
   697  ```expr
   698  join(["apple", "orange", "grape"], ",") == "apple,orange,grape"
   699  join(["apple", "orange", "grape"]) == "appleorangegrape"
   700  ```
   701  
   702  ### reduce(array, predicate[, initialValue]) {#reduce}
   703  
   704  Applies a predicate to each element in the array, reducing the array to a single value.
   705  Optional `initialValue` argument can be used to specify the initial value of the accumulator.
   706  If `initialValue` is not given, the first element of the array is used as the initial value.
   707  
   708  Following variables are available in the predicate:
   709  
   710  - `#` - the current element
   711  - `#acc` - the accumulator
   712  - `#index` - the index of the current element
   713  
   714  ```expr
   715  reduce(1..9, #acc + #)
   716  reduce(1..9, #acc + #, 0)
   717  ```
   718  
   719  ### sum(array[, predicate]) {#sum}
   720  
   721  Returns the sum of all numbers in the array.
   722  
   723  ```expr
   724  sum([1, 2, 3]) == 6
   725  ```
   726  
   727  If the optional `predicate` argument is given, it is a predicate that is applied on each element
   728  of the array before summing.
   729  
   730  ```expr
   731  sum(accounts, .Balance)
   732  ```
   733  
   734  Equivalent to:
   735  
   736  ```expr
   737  reduce(accounts, #acc + .Balance, 0)
   738  // or
   739  sum(map(accounts, .Balance))
   740  ```
   741  
   742  ### mean(array) {#mean}
   743  
   744  Returns the average of all numbers in the array.
   745  
   746  ```expr
   747  mean([1, 2, 3]) == 2.0
   748  ```
   749  
   750  ### median(array) {#median}
   751  
   752  Returns the median of all numbers in the array.
   753  
   754  ```expr
   755  median([1, 2, 3]) == 2.0
   756  ```
   757  
   758  ### first(array) {#first}
   759  
   760  Returns the first element from an array. If the array is empty, returns `nil`.
   761  
   762  ```expr
   763  first([1, 2, 3]) == 1
   764  ```
   765  
   766  ### last(array) {#last}
   767  
   768  Returns the last element from an array. If the array is empty, returns `nil`.
   769  
   770  ```expr
   771  last([1, 2, 3]) == 3
   772  ```
   773  
   774  ### take(array, n) {#take}
   775  
   776  Returns the first `n` elements from an array. If the array has fewer than `n` elements, returns the whole array.
   777  
   778  ```expr
   779  take([1, 2, 3, 4], 2) == [1, 2]
   780  ```
   781  
   782  ### reverse(array) {#reverse}
   783  
   784  Return new reversed copy of the array.
   785  
   786  ```expr
   787  reverse([3, 1, 4]) == [4, 1, 3]
   788  reverse(reverse([3, 1, 4])) == [3, 1, 4]
   789  ```
   790  
   791  ### sort(array[, order]) {#sort}
   792  
   793  Sorts an array in ascending order. Optional `order` argument can be used to specify the order of sorting: `asc`
   794  or `desc`.
   795  
   796  ```expr
   797  sort([3, 1, 4]) == [1, 3, 4]
   798  sort([3, 1, 4], "desc") == [4, 3, 1]
   799  ```
   800  
   801  ### sortBy(array[, predicate, order]) {#sortBy}
   802  
   803  Sorts an array by the result of the [predicate](#predicate). Optional `order` argument can be used to specify the order
   804  of sorting: `asc` or `desc`.
   805  
   806  ```expr
   807  sortBy(users, .Age)
   808  sortBy(users, .Age, "desc")
   809  ```
   810  
   811  ## Map Functions
   812  
   813  ### keys(map) {#keys}
   814  
   815  Returns an array containing the keys of the map.
   816  
   817  ```expr
   818  keys({"name": "John", "age": 30}) == ["name", "age"]
   819  ```
   820  
   821  ### values(map) {#values}
   822  
   823  Returns an array containing the values of the map.
   824  
   825  ```expr
   826  values({"name": "John", "age": 30}) == ["John", 30]
   827  ```
   828  
   829  ## Type Conversion Functions
   830  
   831  ### type(v) {#type}
   832  
   833  Returns the type of the given value `v`.
   834  
   835  Returns on of the following types:
   836  
   837  - `nil`
   838  - `bool`
   839  - `int`
   840  - `uint`
   841  - `float`
   842  - `string`
   843  - `array`
   844  - `map`.
   845  
   846  For named types and structs, the type name is returned.
   847  
   848  ```expr
   849  type(42) == "int"
   850  type("hello") == "string"
   851  type(now()) == "time.Time"
   852  ```
   853  
   854  ### int(v) {#int}
   855  
   856  Returns the integer value of a number or a string.
   857  
   858  ```expr
   859  int("123") == 123
   860  ```
   861  
   862  ### float(v) {#float}
   863  
   864  Returns the float value of a number or a string.
   865  
   866  ```expr
   867  float("123.45") == 123.45
   868  ```
   869  
   870  ### string(v) {#string}
   871  
   872  Converts the given value `v` into a string representation.
   873  
   874  ```expr
   875  string(123) == "123"
   876  ```
   877  
   878  ### toJSON(v) {#toJSON}
   879  
   880  Converts the given value `v` to its JSON string representation.
   881  
   882  ```expr
   883  toJSON({"name": "John", "age": 30})
   884  ```
   885  
   886  ### fromJSON(v) {#fromJSON}
   887  
   888  Parses the given JSON string `v` and returns the corresponding value.
   889  
   890  ```expr
   891  fromJSON('{"name": "John", "age": 30}')
   892  ```
   893  
   894  ### toBase64(v) {#toBase64}
   895  
   896  Encodes the string `v` into Base64 format.
   897  
   898  ```expr
   899  toBase64("Hello World") == "SGVsbG8gV29ybGQ="
   900  ```
   901  
   902  ### fromBase64(v) {#fromBase64}
   903  
   904  Decodes the Base64 encoded string `v` back to its original form.
   905  
   906  ```expr
   907  fromBase64("SGVsbG8gV29ybGQ=") == "Hello World"
   908  ```
   909  
   910  ### toPairs(map) {#toPairs}
   911  
   912  Converts a map to an array of key-value pairs.
   913  
   914  ```expr
   915  toPairs({"name": "John", "age": 30}) == [["name", "John"], ["age", 30]]
   916  ```
   917  
   918  ### fromPairs(array) {#fromPairs}
   919  
   920  Converts an array of key-value pairs to a map.
   921  
   922  ```expr
   923  fromPairs([["name", "John"], ["age", 30]]) == {"name": "John", "age": 30}
   924  ```
   925  
   926  ## Miscellaneous Functions
   927  
   928  ### len(v) {#len}
   929  
   930  Returns the length of an array, a map or a string.
   931  
   932  ```expr
   933  len([1, 2, 3]) == 3
   934  len({"name": "John", "age": 30}) == 2
   935  len("Hello") == 5
   936  ```
   937  
   938  ### get(v, index) {#get}
   939  
   940  Retrieves the element at the specified index from an array or map `v`. If the index is out of range, returns `nil`.
   941  Or the key does not exist, returns `nil`.
   942  
   943  ```expr
   944  get([1, 2, 3], 1) == 2
   945  get({"name": "John", "age": 30}, "name") == "John"
   946  ```
   947  
   948  ## Bitwise Functions
   949  
   950  ### bitand(int, int) {#bitand}
   951  
   952  Returns the values resulting from the bitwise AND operation.
   953  
   954  ```expr
   955  bitand(0b1010, 0b1100) == 0b1000
   956  ```
   957  
   958  ### bitor(int, int) {#bitor}
   959  
   960  Returns the values resulting from the bitwise OR operation.
   961  
   962  ```expr
   963  bitor(0b1010, 0b1100) == 0b1110
   964  ```
   965  
   966  ### bitxor(int, int) {#bitxor}
   967  
   968  Returns the values resulting from the bitwise XOR operation.
   969  
   970  ```expr
   971  bitxor(0b1010, 0b1100) == 0b110
   972  ```
   973  
   974  ### bitnand(int, int) {#bitnand}
   975  
   976  Returns the values resulting from the bitwise AND NOT operation.
   977  
   978  ```expr
   979  bitnand(0b1010, 0b1100) == 0b10
   980  ```
   981  
   982  ### bitnot(int) {#bitnot}
   983  
   984  Returns the values resulting from the bitwise NOT operation.
   985  
   986  ```expr
   987  bitnot(0b1010) == -0b1011
   988  ```
   989  
   990  ### bitshl(int, int) {#bitshl}
   991  
   992  Returns the values resulting from the Left Shift operation.
   993  
   994  ```expr
   995  bitshl(0b101101, 2) == 0b10110100
   996  ```
   997  
   998  ### bitshr(int, int) {#bitshr}
   999  
  1000  Returns the values resulting from the Right Shift operation.
  1001  
  1002  ```expr
  1003  bitshr(0b101101, 2) == 0b1011
  1004  ```
  1005  
  1006  ### bitushr(int, int) {#bitushr}
  1007  
  1008  Returns the values resulting from the unsigned Right Shift operation.
  1009  
  1010  ```expr
  1011  bitushr(-0b101, 2) == 4611686018427387902
  1012  ```