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.

rand has bin in within div mod bar rand cmb msum mavg count first last sum min max *[avg var dev med ..]; key meta

exp log sin cos sqr sqrt prm freq sums deltas
rand has bin in within div mod bar cmb msum mavg
count first last sum min max

9.1 exp ⇒ exp x

exp compute the exponent of x, i.e. ex.

 exp 1
2.718282

9.2 log ⇒ log x

Log computes the natural log.

 log 10
2.302585

9.3 sin ⇒ sin x

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

 sin 0
0f
 sin 3.1416%2
1.

9.4 cos ⇒ cos x

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

 cos 0
1f
 cos 3.1416%4
0.7071055

9.5 sqr ⇒ sqr x

Compute the square of x.

 sqr 2
4.0

9.6 sqrt ⇒ sqrt x

Compute the square root of x.

 sqrt 2
1.414214

9.7 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.8 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.9 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.10 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.11 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.12 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.13 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.14 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.15 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.16 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.17 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.18 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.19 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.20 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.21 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.22 count ⇒ count x

Same as count.

 count 5 1 2
3

9.23 first ⇒ first x

Same as first.

 first 5 1 2
5

9.24 last ⇒ last x

Retrieve the last element of list x.

 last 5 1 2
2

9.25 sum ⇒ sum x

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

 sum 5 1 2
8

9.26 min ⇒ min x

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

 min 5 1 2
1

9.27 max ⇒ max x

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

 max 5 1 2
5

9.28 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.29 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.30 unkey ⇒ unkey x

Remove key from table x.

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

9.31 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