Skip to content

Latest commit

 

History

History
295 lines (224 loc) · 5.23 KB

example.md

File metadata and controls

295 lines (224 loc) · 5.23 KB

Examples

Evaluate an expression

$ arrai eval '41 + 1'
42

Evaluate count of string

$ arrai eval '"123456789" count'
9

Evaluate a collection of values

$ arrai eval '[1,2,3,4] >> .^2'
[1,4,9,16]
$ arrai eval '{1,2,3,4} => .^2'
{1,4,9,16}

Filter a collection of values

$ arrai eval '{(a:1, b:2), (a:2, b:3), (a:2, b:4)} where .a=2'
{(a:2, b:4)}

Operations for filtering a stream of values

$ arrai eval '{(a:1, b:2), (a:2, b:3), (a:2, b:4)} where .a=2 and .b=3'
{(a:2, b:3)}
$ arrai eval '{(a:1, b:2), (a:2, b:3), (a:2, b:4)} where .a=2 or .b=4'
{(a:2, b:3), (a:2, b:4)}
$ arrai eval '{(a:1, b:2), (a:2, b:3), (a:2, b:4)} where .a!=2'
{(a:1, b:2)}

Conditional operator

Standard Cases

$ arrai eval 'cond { 2 > 1 : 1, 2 > 3 : 2}'
1

Note: Trailing comma is allowed

$ arrai eval 'cond {
    2 > 1 : 1,
    2 > 3 : 2,
}'
1
$ arrai eval 'cond { 2 > 1 : 1, 2 > 3 : 2, _ : 3}'
1
$ arrai eval 'cond { 2 < 1 : 1, 2 > 3 : 2, _ : 3}'
3
$ arrai eval 'let a = cond { 2 > 1 : 1, 2 > 3 : 2, _ : 3};a * 3'
3
$ arrai eval 'let a = cond { 2 < 1 : 1, 2 > 3 : 2, _ : 3};a * 3'
9
$ arrai eval 'let a = cond { 1 < 2 : 1, 2 > 3 : 2, _ : 3};a * 3'
3
$ arrai eval 'let a = cond { 2 < 1 : 1, 2 < 3 : 2, _ : 3};a * 3'
6
$ arrai eval 'let a = cond { 2 < 1 || 1 > 0 : 1, 2 < 3 : 2, _ : 3};a * 3'
3
$ arrai eval 'let a = cond { 2 < 1 || 1 > 2 : 1, 2 < 3 && 1 > 0 : 2, _ : 3};a * 3'
6
$ arrai eval 'cond {cond {1 > 0 : 1} > 0 : 1, 2 < 3: 2, _:1 + 2}'
1
$ arrai eval 'cond {cond {1 > 2 : 1, _ : 11} < 2 : 1, 2 < 3: 2, _:1 + 2}'
2

Control Var Cases

$ arrai eval 'let a = 1; cond a {1 :1, 2 :2, _:1 + 2}'
1
$ arrai eval 'let a = 1; cond a {1 :1 + 10, 2 : 2, _:1 + 2}'
11
$ arrai eval 'let a = 1; cond a {2 :2, _:1 + 2}'
3
$ arrai eval 'let a = 1; let b = cond a {1 :1, 2 :2, _:1 + 2}; b * 100'
100
$ arrai eval 'let a = 1; cond a + 1 {1 :1, 2 :2, _:1 + 2}'
2
$ arrai eval 'let a = 2; cond a { 1: "A", (2, 3): "B", _: "C"}'
B
$ arrai eval 'let a = 2; cond a { (cond a {(1,2) : 1}): "A", (2, 3): "B", _: "C"}'
B
$ arrai eval 'let a = 1; cond a { (cond {2 > 1 : 1}): "A", (2, 3): "B", _: "C"}'
A
$ arrai eval 'let a = 1; cond { cond a {1 : 1} : "A", 2: "B", _: "C"}'
A

Filter a collection of values with Control Var cases

$ arrai eval '{1, [2, 3], 4, [5, 6]} filter . {[a, b]: a + b}'
{5, 11}
$ arrai eval '{1, [2, 3], 4, [5, 6], [7, 8, 9]} filter . {[a, ..., b]: a + b}'
{5, 11, 16}
$ arrai eval '{1, [2, 3], 4, [5, 6]} filter . {[_, _]: 42}'
{42}

Transform JSON and filter a value from it

Filter out value of a from JSON:

$ echo '{"a": "hello", "b": "world"}'| arrai json | arrai x '.("a")'
"hello"

Nest

Nest groups the given attributes into a nested relation, keys on the given key.

relation nest |attr1,attr2,...|key

Examples

$ arrai eval '{|a,b| (1,2), (1,3), (2,4)} nest |b|nested-b'
{(a: 1, nested-b:{(b: 2), (b: 3)}), (a: 2, nested-b: {(b: 4)})}
$ arrai eval '{|a,b| (1,2), (2,3)} nest |b|nestb'
{(a: 1, nestb:{(b: 2)}), (a: 2, nestb:{(b: 3)})}
$ arrai eval '{|a,b,c| (1,2,3), (1,3,3), (1,2,2)} nest |b|nestb'
{(a: 1, c: 3, nestb: {(b: 2), (b: 3)}), (a: 1, c: 2, nestb: {(b: 2)})}
$ arrai eval '{|a,b,c| (1,2,3), (1,3,3), (1,2,2)} nest |b,c|nestbc'
{(a: 1, nestbc: {(b: 2, c: 3), (b: 3, c: 3), (b: 2, c: 2)})}

Nest collects all the b values grouped by the common a values.

Join

Join takes two relations and joins them by matching tuples on common attribute names.

Examples

$ arrai eval '{|a,b| (1,2), (2,2)} <&> {|a,c| (1,2), (1,3)}'
{(a: 1, b: 2, c: 2), (a: 1, b: 2, c: 3)}
$ arrai eval '{|a,b| (1,2), (2,2)} <&> {|a,c| (1,2), (1,3), (2,3)}'
{(a: 1, b: 2, c: 2), (a: 1, b: 2, c: 3), (a: 2, b: 2, c: 3)}
$ arrai eval '{|a,b| (1,2), (1,3)} <&> {|a,c| (1,2), (1,3)}'
{(a: 1, b: 2, c: 2), (a: 1, b: 2, c: 3), (a: 1, b: 3, c: 2), (a: 1, b: 3, c: 3)}
$ arrai eval '{|a,b,c| (1,2,2), (1,2,3)} <&> {|b,c,d| (2,3,4), (1,3,4)}'
{(a: 1, b: 2, c: 3, d: 4)}

Merge

Merge combines two tuples/dicts, producing a single tuple/dict containing a union of their attributes. If the same name is present in both the LHS (left-hand side) and RHS (right-hand side) tuples/dicts, the RHS value takes precedence in the output.

Examples

$ arrai e '(a: 1, b: 2) +> (b: 3, c: 4)'
(a: 1, b: 3, c: 4)
$ arrai e '(a: 1, b: (c: 2)) +> (b: (c: 4), c: 4)'
(a: 1, b: (c: 4), c: 4)
$ arrai e '(a: (b: 1)) +> (a: (c: 2))'
(a: (c: 2))
$ arrai e '{"a": 1, "b": 2} +> {"b": 3, "d": 4}'
{'a': 1, 'b': 3, 'd': 4}
$ arrai e '{"a": {"b": 1}} +> {"a": {"c": 2}}'
{'a': {'c': 2}}