Function Parameters
Function Parameters: K
Some functions have parameters that are either optional or required. For example, e() has an optional parameter that is written as: eK(). The k (defaulting to 1) represents how many elements are looked at in each pass through.
from ArrayExpressions import arrex
lst = [1, 2, 3, 4, 5, 6]
output = arrex.evaluate(lst, "e2(i0(x) + i1(x))")
print(output) # [3, 7, 11]
Step | X | Code | Return | Comments |
---|---|---|---|---|
1 | [1, 2, 3, 4, 5, 6] | e2(i0() + i1()) | Nothing returned yet | |
2 | [1, 2] | i0(x) | 1 | Gets the first element of x |
3 | [1, 2] | i1(x) | 2 | Gets the second element of x |
4 | [1, 2] | i0(x) + i1(x) | 3 | Adds the two elements |
5 | [3, 4] | i0(x) | 3 | Gets the first element of x |
6 | [3, 4] | i1(x) | 4 | Gets the second element of x |
7 | [3, 4] | i0(x) + i1(x) | 7 | Adds the two elements |
8 | [5, 6] | i0(x) | 5 | Gets the first element of x |
9 | [5, 6] | i1(x) | 6 | Gets the second element of x |
10 | [5, 6] | i0(x) + i1(x) | 11 | Adds the two elements |
11 | [1, 2, 3, 4, 5, 6] | [3, 7, 11] | [3, 7, 11] | The code was replaced with an array |
In this, pairs of elements were added together. The parameter (k) can be distinguished for readability using curly brackets ({}). For example: 'e{2}(i0() + i1())' is the same as 'e2(i0() + i1())'. Curly brackets also allow you to make more complex parameters. For example, you could write e{(L - 3) / 2}(x) which isn't possible without curly brackets (L refers to the length of 'x').