Next: , Previous: , Up: Top

## 9 Named Functions

This chapter covers the built-in named functions. This includes some math (eg. sqrt but not +), wrapper (eg. count for #) and range (eg. within) functions.

```count first last min max sum dot avg var [dev med mode countd ..]
sqrt sqr exp log sin cos div mod bar in
```

### 9.1 count ⇒ count x

Same as count.

``` count 5 1 2
3
```

### 9.2 first ⇒ first x

Same as first.

``` first 5 1 2
5
```

### 9.3 last ⇒ last x

Retrieve the last element of list x.

``` last 5 1 2
2
```

### 9.4 min ⇒ min x

Retrieve the minimum element of list x. Same as `|/`.

``` min 5 1 2
1
```

### 9.5 max ⇒ max x

Retrieve the maximum element of list x. Same as `&/`.

``` max 5 1 2
5
```

### 9.6 sum ⇒ sum x

Compute the sum of list x. Same as `+/`.

``` sum 5 1 2
8
```

### 9.7 dot ⇒ dot x

Compute the dot product of list x. Same as `+/x*x`.

``` dot 1.2 1.3 1.4
5.09

{+/x*x} 1.2 1.3 1.4
5.09
```

### 9.8 avg ⇒ avg x

Compute the average of list x. Same as `+/x%#x`.

``` avg 2 4 7 10
5.75

{+/x%#x} 2 4 7 10
5.75
```

### 9.9 var ⇒ var x

Compute the variance of list x.

``` var !10
8.25

{(+/x*x:x-avg x)%#x}@!10
8.25
```

TBD

TBD

TBD

TBD

### 9.14 exp ⇒ exp x

Compute the exponent of x, i.e. ex.

``` exp 1
2.718282
```

### 9.15 log ⇒ log x

Log computes the natural log.

``` log 10
2.302585
```

### 9.16 sin ⇒ sin x

sin computes the sine of x where x is in radians.

``` sin 0
0f
sin 3.1416%2
1.
```

### 9.17 cos ⇒ cos x

cos computes the cosine of x where x is in radians.

``` cos 0
1f
cos 3.1416%4
0.7071055
```

### 9.18 sqr ⇒ sqr x

Compute the square of x.

``` sqr 2
4.0
```

### 9.19 sqrt ⇒ sqrt x

Compute the square root of x.

``` sqrt 2
1.414214
```

### 9.20 prm ⇒ prm x

Write out all permutations of integers up x-1. Display them as columns.

``` prm 3
0 1 1 0 2 2
1 0 2 2 0 1
2 2 0 1 1 0
```

### 9.21 freq ⇒ freq x

Compute the frequency of the items in list x.

``` freq "abzccdefeajfekjrlke"
a|2
b|1
c|2
d|1
e|4
f|2
j|2
k|2
l|1
r|1
z|1

freq 1000 rand 10
0| 95
1|106
2| 84
3| 98
4|109
5|104
6| 94
7| 95
8|120
9| 95
```

### 9.22 sums ⇒ sums x

Compute the running sum of list x. Sames as `+\`.

``` sums !10
0 1 3 6 10 15 21 28 36 45
```

### 9.23 deltas ⇒ deltas x and x deltas y

Compute the difference between each element in list x and the previous value. If delta is called with two parameters then x will be used as the first value to delta instead of the default 0.

``` deltas 1 5 10
1 4 5

1 deltas 1 5 10
0 4 5
```

### 9.24 rand ⇒ rand x or x rand y

Generate x random values using a uniform distribution from [0,1) or -x random values of a normal distribution (mean=0, standard deviation=1).

``` rand 5  / uniform
0.9722755 0.8904738 0.4088624 0.1399796 0.1334422

rand -5 / normal
1.008461 0.6204548 -1.206095 -0.7168573 -0.5728272
```

Generate x random values from element or list y. For element y, y is the exclusive upper bound (int, float, date, or time), the type (character), or the length of each element (name). For list y, rand choose between elements of the list.

``` 5 rand 10 / values between 0 and 9
3 0 0 7 8

4 rand "a" / values between "A" and "P"
"OGFN"

4 rand `3 / name of length 3 using letters between "A" and "P"
`BFH`HKH`LOO`DAC

5 rand 1 2
1 2 2 1 1

5 rand "xy"
"yxxxy"
```

### 9.25 has ⇒ x has y

Determine whether vector x has element y. Simliar to in but with the arguments reversed.

``` `a`b`c`a`b has `a
1b

`a`b`c`a`b has `d
0b

`a`b`c`a`b has `a`b`x`y`z
11000b

(1 2;4 5 6;7 9)has(1 2;8 9)
10b
```

### 9.26 bin ⇒ x bin y

Given a sorted (increasing) list x, find the greatest index, i, where y>x[i].

``` n:exp 0.01*!5;n
1 1.01005 1.020201 1.030455 1.040811
1.025 bin n
2
```

### 9.27 in ⇒ x in y

Determine if x is in list y. Similar to has but arguments reversed.

``` `b in `a`b`d`e
1b
`c in `a`b`d`e
0b
```

### 9.28 within ⇒ x within y

Test if x is equal to or greater than y[0] and less than y[1].

``` 3 within 0 12
1b

0 within 0 12
1b

12 within 0 12
0b

23 within 0 12
0b
```

### 9.29 div ⇒ x div y

y divided by x using integer division, taking the floor of the result. x and y must be integers.

``` 2 div 7
3
5 div 22
4
```

### 9.30 mod ⇒ x mod y

The remainder after y divided by x using integer division. x and y must be integers.

``` 12 mod 27
3
5 mod 22
2
```

### 9.31 bar ⇒ x bar y

For each value in y determine the maximum multiple of x that is less than or equal to each y.

``` 10 bar 9 10 11 19 20 21
0 10 10 10 20 20
```

### 9.32 cmb ⇒ x cmb y

Determine all x value combinations up to y. In this case these are ordered in ascending order from left to right.

``` 3 cmb 5
2 3 4
1 3 4
1 2 4
1 2 3
0 3 4
0 2 4
0 2 3
0 1 4
0 1 3
0 1 2

`a`b`c`d`e@3 cmb 5
`c`d`e
`b`d`e
`b`c`e
`b`c`d
`a`d`e
`a`c`e
`a`c`d
`a`b`e
`a`b`d
`a`b`c
```

### 9.33 msum ⇒ x msum y

Compute the length x moving sum of list y.

``` 3 msum !10
0 1 3 6 9 12 15 18 21 24
```

### 9.34 mavg ⇒ x mavg y

Compute the length x moving average of list y.

``` 3 mavg !10
0 0.3333333 1 2 3 4 5 6 7 8
```

### 9.35 top ⇒ top x

Return the index of the max value of list x.

``` top 3 13 15 1 17 0 -3
4

top "abzccdefeajfekjrlke"
2
```

### 9.36 key ⇒ x key y

Key table y with x. That is, the values in the x column become the key values.

``` `a key [[]a:`fa`fb;b:1 2]
a |b
--|-
fa|1
fb|2

`a key [[]a:`fa`fb;b:((1 2 3); (4 5 6))]
a |b
--|-----
fa|1 2 3
fb|4 5 6
```

### 9.37 unkey ⇒ unkey x

Remove key from table x.

``` unkey [[a:`x`y]b:1 2]
a b
- -
x 1
y 2
```

### 9.38 meta ⇒ meta

Determine the types of the fields in a table.

``` meta [[]a:`x`y;b:1 2]
a|n
b|i
```

Next: , Previous: , Up: Top