# Q by examples

A quick tour of q through short, runnable examples. Each example is shown as typed at the `q)` prompt, followed by its result. Start q, paste a line and compare.

Q evaluates expressions right to left, with no operator precedence. Keep that in mind when reading the examples.

Examples that use random numbers return different values each time you run them.

## Simple arithmetic
```q
q)2+2           / comment is ' /': left of /: whitespace or nothing
4

q)2-3           / negative numbers
-1

q)2*3+4         / no precedence, right to left
14

q)(2*3)+4       / parentheses change order
10

q)3%4           / division
0.75

q){x*x}4        / square
16

q)sqrt 4        / square root
2f

q)reciprocal 4  / 1/x
0.25
```

## Operations using lists
```q
q)2*1 2 3      / numeric list with space separators
2 4 6

q)1 2 3%2 4 6  / list-to-list operations, same size
0.5 0.5 0.5

q)count 1 2 3  / items in a list
3

q)3#1          / generate sequence of same numbers
1 1 1

q)5#1 2        / or from a list of given items
1 2 1 2 1
```

## List items
```q
q)first 1 2 3     / first item
1

q)last 1 2 3      / last item
3

q)1_1 2 3         / rest without first item
2 3

q)-1_1 2 3        / rest without last item
1 2

q)reverse 1 2 3   / reverse
3 2 1
```

## Indexing and sorting
```q
q)1 2 3@1       / indexing is zero-based
2

q)1 2 3@1 0     / index can be vector too
2 1

q)til 3         / generate zero-based sequence
0 1 2

q)2 4 6?4       / index of given item/s
1

q)iasc 2 1 6    / indexes of sorted order
1 0 2

q)asc 2 1 6     / sort vector
`s#1 2 6
```

The <code>\`s#</code> prefix is the sorted attribute, which q sets on the result of `asc`.

## List aggregation
```q
q)1 2 3,10 20         / join lists
1 2 3 10 20

q)1+2+3               / sum of items
6

q)sum 1 2 3           / insert '+' between items
6

q)sums 1 2 3          / running sum of items
1 3 6

q)1,(1+2),(1+2+3)     / same as this
1 3 6

q){1_x+prev x}til 5   / sum running pairs
1 3 5 7

q)sum each{(2*til ceiling .5*count x)_x}1 2 3 4 5 / non-intersecting pairs
3 7 5

q)(1 2;3 4 6;7 6)     / list
1 2
3 4 6
7 6

q)first(3 4 6;7 6)    / first item in the list
3 4 6
```

## Function combinations
```q
q){x+x*x}4              / a + a^2
20

q)(sqrt;{x*x})@\:4      / [sqrt(a), a^2]
2f
16

q){x*x}sum 2 3          / (a +b)^2
25

q)sum{x*x}2 3           / a^2 + b^2
13

q){sum(x*x),2*/x}2 3    / (a + b)^2 = a^2 + b^2 + 2ab
25

q)sqrt sum{x*x}3 4      / sqrt(a^2 + b^2)
5f
```

## User-defined functions and arguments
```q
q)d1:-                  / binary projection
q)d2:{x-y}              / explicit binary
q)m1:neg                / unary projection
q)m2:0-                 / unary projection
q)m3:{neg x}            / explicit unary

q)(m1;m2;m3)@\:4        / unary functions
-4 -4 -4

q)(d1;d2).\:3 4         / binary functions
-1 -1
```

## Exponent and logarithm
```q
q)(e;2*e;e*e:exp 1)    / e, 2e, e squared
2.718282 5.436564 7.389056

q)exp 2               / exponent, e^2
7.389056

q)2 xexp 16           / exponent base 2, 2^16
65536f

q)log exp 2           / logarithm, ln e^2
2f

q)2 xlog 65536        / logarithm base 2, log2 65536
16f
```

## Trigonometry
```q
q)a:(pi;2*pi;pi*pi:acos -1)     / pi, 2 pi, pi squared
3.141593 6.283185 9.869604

q)cos pi                        / cosine of pi
-1f

q)(t:sum{x*x}@(cos;sin)@\:)pi   / theorem of trigonometry
1f

q)t a                           / test theorem at angles
1 1 1f
```

## Matrixes
```q
q)1 2 3*/:1 2 3                 / outer product: multiplication table
1 2 3
2 4 6
3 6 9

q){x=/:x}@til 3                 / identity matrix
100b
010b
001b

q)2 3#til 6                     / generate matrix
0 1 2
3 4 5

q)2 2#0 1 1 1                   / reshape vector to matrix
0 1
1 1
```

## Structural transforms
```q
q)show N:0 3_/:2 6#til 12                            / list of atoms
0 1 2   3 4 5
6 7  8  9 10 11
q)raze/[N]                                           / ravel
0 1 2 3 4 5 6 7 8 9 10 11

q)raze each N                                        / ravel each sub-matrix
0 1 2 3 4  5
6 7 8 9 10 11

q)show M:3 3#"ABC123!@#"                             / character matrix
"ABC"
"123"
"!@#"
q)(::;flip;reverse;reverse each;1 rotate)@\:M
"ABC" "123" "!@#"
"A1!" "B2@" "C3#"
"!@#" "123" "ABC"
"CBA" "321" "#@!"
"123" "!@#" "ABC"

q)M ./:/:f value group sum each f:n cross n:til 3    / secondary diagonals
,"A"
"B1"
"C2!"
"3@"
,"#"

q)M ./:a,'a:til count M                              / main diagonal
"A2#"
```

## Selection
```q
q)N:((0 1 2;3 4 5);(6 7 8;9 10 11))
q)((N 1) 1) 1           / repetitive selection of items From list
10

q)3@[;1]/N              / apply select 3 times
10

q)N[1;1;1]              / cross sectional select
10

q)N . 1 1 1             / cross sectional select too
10
```

## Factorial and binomial
```q
q)each[f:{$[x<0;0;prd 1.+til x]}]1+til 5                    / factorial
1 2 6 24 120f

q)prds 1+til 5                                              / running product
1 2 6 24 120

q)(b:{til[x]{$[x<y;0;floor f[x]%f[y]*f x-y]}\:/:til x})5    / binomial coeff.
1 1 1 1 1
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1

q)/ fibonacci: sum of second diagonal of binomial matrix
q)1_{sum b[x]./:flip(til x;reverse til x)}each til 16
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
```

## Dot product
```q
q)1 2 3 wsum 1 2 3       / dot product wsum=+/* (optimized)
14f

q)1 2 3.$1 2 3.          / also
14f

q)M:(0 1.;1 1.)          / assignment
q)M$M                    / matrix squared (optimized)
1 1
1 2

q)15$[M]/M               / matrix to the power of 15, also fibonacci
610 987
987 1597

q)(14$[M]\M)[;0;1]
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610f
```

## Randomness and probability
```q
q)A:5?1.;A                 / 5 random floats from 0..1
0.3927524 0.5170911 0.5159796 0.4066642 0.1780839

q)B:10?2;B                 / coin toss
1 0 1 0 1 0 1 1 0 0

q)B1:10?0b;B1              / with booleans
1101010111b

q)C:-3?3;C                 / deal 3 unique cards out of 3
0 2 1

q)(min;max)@\:A            / min and max over the list
0.1780839 0.5170911

q)B?0                      / first zero
1

q)avg C~/:1_10000{-3?3}\() / method monte carlo
0.1662

q)reciprocal f 3           / exact probability of 3 cards in given order
0.1666667
```

## Unique elements
```q
q)D:distinct S:"mississippi"                / distinct items
"misp"

q)K:D?S;K                                   / find (?) indexes
0 1 2 2 1 2 2 1 3 3 1

q)S value group K                           / group by key
,"m"
"iiii"
"ssss"
"pp"

q)count each group S                        / frequencies
m| 1
i| 4
s| 4
p| 2

q)I:(til count S)in first each group S;I    / sieve of nub where D is in S
11100000100b

q)S where I                                 / filter by sieve to get D
"misp"

q)sum D=/:S                                 / where items of D are in S
1 4 4 2i
```

## Set operations

[`inter`](../ref/inter.md), [`except`](../ref/except.md) and [`union`](../ref/union.md) treat lists as sets. They work on lists of any type.
```q
q)1 2 3 4 inter 3 4 5          / items in both lists
3 4

q)1 2 3 4 except 3 4 5         / items of the left list not in the right
1 2

q)1 2 3 4 union 3 4 5          / items in either list, without duplicates
1 2 3 4 5

q)1 2 2 3 union 3 3 4          / union also removes duplicates within a list
1 2 3 4

q)`a`b`c inter `b`c`d          / works with symbols
`b`c

q)"hello" except "l"           / and with strings
"heo"
```

## Dictionaries

A dictionary maps a list of keys to a list of values. When the keys are symbols, use the dictionary literal syntax `([key:value;…])`. For other key types, use [`!`](../ref/dict.md), as in `` `a`b`c!1 2 3 ``.
```q
q)d:([a:1;b:2;c:3])    / create a dictionary
q)d
a| 1
b| 2
c| 3

q)d`b                  / look up a key
2

q)key d                / keys
`a`b`c

q)value d              / values
1 2 3

q)d,([d:4])            / join dictionaries
a| 1
b| 2
c| 3
d| 4

q)([a:1;b:2 3;c:"x"]) / values can be of different types
a| 1
b| 2 3
c| "x"
```

## Tables

A table is a list of dictionaries with the same keys, stored as a flip of a dictionary of column lists. Query tables with [`select`](../ref/select.md), [`exec`](../ref/exec.md) and [`update`](../ref/update.md).
```q
q)flip([a:1 2;b:3 4])                         / table from a dictionary of columns
a b
---
1 3
2 4

q)t:([]sym:`a`b`a`c;px:10 20 30 40f;qty:1 2 3 4)
q)t
sym px qty
----------
a   10 1
b   20 2
a   30 3
c   40 4

q)select from t where sym=`a                  / filter rows
sym px qty
----------
a   10 1
a   30 3

q)select sum qty,avg px by sym from t         / aggregate by group
sym| qty px
---| ------
a  | 4   20
b  | 2   20
c  | 4   40

q)update px2:px*2 from t                      / add a column
sym px qty px2
--------------
a   10 1   20
b   20 2   40
a   30 3   60
c   40 4   80

q)exec px from t                              / column as a list
10 20 30 40f

q)cols t                                      / column names
`sym`px`qty

q)`px xdesc t                                 / sort by column, descending
sym px qty
----------
c   40 4
a   30 3
b   20 2
a   10 1
```

## Iterators

[Iterators](../ref/iterators.md) such as `each`, `over` (`/`) and `scan` (`\`) apply a function to the items of a list.
```q
q){x*2}each 1 2 3     / apply to each item
2 4 6

q)(+/)1 2 3           / over: reduce with +
6

q)(+\)1 2 3           / scan: running reduce
1 3 6

q){x+y}/[10;1 2 3]    / over with an initial value
16

q)1 2 3+\:10 20       / each-left: each item on the left with the whole right
11 21
12 22
13 23

q)1 2 3+/:10 20       / each-right: whole left with each item on the right
11 12 13
21 22 23
```

## Strings and symbols

A string is a list of characters. A symbol, such as `` `abc ``, is an atom. Use symbols for names and repeated labels and strings for text.
```q
q)upper "hello"                  / strings are character lists
"HELLO"

q)"hello",". world"              / join strings
"hello. world"

q)"," vs "a,b,c"                 / split a string
,"a"
,"b"
,"c"

q)ssr["hello world";"world";"q"] / search and replace
"hello q"

q)string `abc                    / symbol to string
"abc"

q)`$"abc"                        / string to symbol
`abc

q)`a`b`c,`d                      / symbol lists join like any list
`a`b`c`d
```

## Dates and times
```q
q)2024.01.15+1                   / date arithmetic, days
2024.01.16

q)`month$2024.01.15              / cast date to month
2024.01m

q)12:30:00.000+00:15             / time arithmetic, minutes
12:45:00.000

q)7 xbar 0 3 7 10 15             / bucket values, e.g. times into intervals
0 0 7 7 14
```

## Source

Based on [J by Example](http://olegykj.sourceforge.net) 06/11/2005 © Oleg Kobchenko
