This chapter may be one of the most difficult for users new to array langauges but one must master adverbs to exploit the full power of k9. Nouns and verbs naturally come but adverbs need to be learned so that one can reach for them when needed.
Adverb f' each [x]f/ over (c/join) [x]f\ scan (c\split) [x]f': eachp f/: eachr g/:over f\: eachl g\:scan
Adverbs modify verbs to operative iteratively over nouns. This previous sentence likely will make sense only once you understand it so let’s jump into an example. Imagine you have a list of 4 lists. Using first
you will retrieve the first sub-list. In order to retrieve the first element of each sub-list you’ll have to modify the function first
with a modifier each
.
x:(1 2; 3 4; 5 6; 7 8);x 1 2 3 4 5 6 7 8 *x / first 1 2 *'x / each first 1 3 5 7
Adverbs area also used where one might use a for
, do
, or while
loop to calculate over a list of value or recursively until a condition is met.
f:{x*y} f\1 .1 .1 .1 / f\ scan over list 1 0.1 0.01 0.001 g:{x*x} g\:.99 / g\: scan until convergence 0.99 0.9801 0.960596 .. 8.87948e-287 0
Apply function f to each value in list x in a single thread. Multithread operation is done with eachp.
*((1 2 3);4;(5 6);7) / first element of the list 1 2 3 *'((1 2 3);4;(5 6);7) / first element of each element 1 4 5 7 !3 / simple list 0 1 2 +/!3 / sum of the list 3 2#'!3 / take 5 copies of the list 0 0 1 1 2 2 +/2#'!3 / sum 3 3 +/'2#'!3 / sum each, eg. (0 1 2)+(0 1 2) 0 2 4
Over has the same function as scan but returns only the last value.
Compute f[x;y] such that fi=f[fi-1;xi].
i | x | f[x] | a f[x] | x:0 1 4 9 16 25 | +\x | 3+\x | +/x | 3+/x |
0 | x0 | x0 | f0[a;x0] | 0 | 0 | 3 | ||
1 | x1 | f1[f0;x1] | f1[f0;x1] | 1 | 1 | 4 | ||
.. | .. | .. | .. | .. | .. | .. | ||
j | xj | fj[fj-1;xj] | fj[fj-1;xj] | .. | .. | .. | ||
.. | .. | .. | .. | .. | .. | .. | ||
n-2 | xn-2 | fn-2[fn-3;xn-2] | fn-2[fn-3;xn-2] | 16 | 30 | 33 | ||
n-1 | xn-1 | fn-1[fn-2;xn-1] | fn-1[fn-2;xn-1] | 25 | 55 | 58 | 55 | 58 |
Examples
(,\)("a";"b";"c") / scan and cat to build up a list a ab abc +\1 20 300 / scan and plus to sum over a list 1 21 321 {y+10*x}\1 20 300 / scan and user func 1 30 600 {y+10*x}/1 20 300 / over and user func 600 x:1 2 3 / x+y0, +y1, +y2 y:10 10 10 x+\y 11 12 13 21 22 23 31 32 33 {x+.5*y}\[1 2 3;20 20 20] / f\x syntax 11 12 13 21 22 23 31 32 33 1 2 3{x+.5*y}\20 20 20 / x f\ syntax 11 12 13 21 22 23 31 32 33
Join a list of strings x using character c between each pair.
"-"/("Some";"random text";"plus";,".") "Some-random text-plus-."
The notaion c/x converts a list of integers x via base c into base 10. In the example below, assuming 2 0 1 are digits in base 8, they convert to 1 + (0*8) + (2*8*8) = 129
8/2 0 1 129
Split the string x using character c to determine the locations.
" "\"Here is a short sentence." Here is a short sentence.
c\x splits a number x into base c digits.
8\129 2 0 1
Apply f[x;y] using the prior value of y, eg. f[yi;yi-1]. f0 returns f[y0;x].
,':[`x;(`$"y",'$!5)] y0 x y1 y0 y2 y1 y3 y2 y4 y3 %':[100;100 101.9 105.1 102.3 106.1] / compute returns 1 1.019 1.031403 0.9733587 1.037146 100%':100 101.9 105.1 102.3 106.1 / using infix notation 1 1.019 1.031403 0.9733587 1.037146
Additionally eachp (’:) is a multithreaded (parallel) each. This will allow a function to run using multiple threads over a list of arguments.
\t {L:xrand 1.;sum L%sin L*cos L*sin L+L*L}':8#_50e6 / multiple threads 831 \t {L:xrand 1.;sum L%sin L*cos L*sin L+L*L}'8#_50e6 / single thread 1202
Apply f[x;y] to each value of y, i.e. the right one.
(!2)+/:!3 0 1 1 2 2 3 {x+10*y}/:[!2;!3] 0 1 10 11 20 21
Apply f[x;y] to each value of x, i.e. the left one.
(!2)+\:!3 0 1 2 1 2 3 {x+10*y}\:[!2;!3] 0 10 20 1 11 21
Converge Over is the same function as Converge Scan but only returns the final value(s).
r:((1;2);3;(,4));r 1 2 3 ,4 ,//:r / raze over the list until it converges to a flat list 1 2 3 4
Compute function f where fi=f[fi-1] and f0=x. If n is specified then it is either (DO) the number of iterations or (WHILE) a function returning a non-zero result.
{x*x}\:.99 / converge to value 0.99 0.9801 0.9605961 ... 1.323698e-18 1.752177e-36 0. (3;{x*x})\:.99 / do 3 times, return 4 values (first is initial) 0.99 0.9801 0.960596 0.9227448 (.5<;{x*x})\:.99 / while .5 < r, i.e. r is greater than 0.5 0.99 0.9801 0.960596 0.9227448 0.8514579 0.7249806 0.5255968 0.276252 (4;{x,x+/x+1})\:,0 / do function 4 times ,0 0 1 0 1 3 4 0 1 3 4 12 13 15 16 0 1 3 4 12 13 15 16 72 73 75 76 84 85 87 88 (10>#;{x,x+/x+1})\:,0 / while output is less than 10 long ,0 0 1 0 1 3 4 0 1 3 4 12 13 15 16 0 1 3 4 12 13 15 16 72 73 75 76 84 85 87 88 x:3 4 2 1 4 99 / until output is x x\:0 0 3 1 4 / the latter example worked out manually x 0 3 x 3 1 x 1 4 x 4 / x[4]=4 so will stop 4