Row-wise macros

Row-wise macros

Row-wise macros allow using symbols to refer to fields of a row. The order of the arguments is always the same: the first argument is the table and the last argument is the expression (can be a begin ... end block). If the table is omitted, the macro is automatically curried (useful for piping).

Shared features across all row-wise macros:

Modify data in place

@byrow!(d, x)

Apply the expression x row by row in d (to modify d in place). Symbols refer to fields of the row. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a). Use cols(c) to refer to field c where c is a variable that evaluates to a symbol. c must be available in the scope where the macro is called.

Examples

julia> t = table((a = [1,2,3], b = ["x","y","z"]));

julia> @byrow! t :b = :b*string(:a)
Table with 3 rows, 2 columns:
a  b
───────
1  "x1"
2  "y2"
3  "z3"

julia> @byrow! t begin
       :a = :a*2
       :b = "x"^:a
       end
Table with 3 rows, 2 columns:
a  b
───────────
2  "xx"
4  "xxxx"
6  "xxxxxx"
source

Apply a function

JuliaDBMeta.@mapMacro.

@map(d, x)

Apply the expression x row by row in d: return the result as an array or as a table (if the elements are Tuples or NamedTuples). Use {} syntax for automatically named NamedTuples. Symbols refer to fields of the row. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a). Use cols(c) to refer to field c where c is a variable that evaluates to a symbol. c must be available in the scope where the macro is called.

Examples

julia> t = table((a = [1,2,3], b = ["x","y","z"]));

julia> @map t :b*string(:a)
3-element Array{String,1}:
 "x1"
 "y2"
 "z3"

julia> @map t {:a, copy = :a, :b}
Table with 3 rows, 3 columns:
a  copy  b
────────────
1  1     "x"
2  2     "y"
3  3     "z"
source

Add or modify a column

@transform(d, x)

Apply the expression x row by row in d: collect the result as a table (elements returned by x must be NamedTuples) and merge it horizontally with d. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a).

Use {} syntax for automatically named NamedTuples. Use cols(c) to refer to field c where c is a variable that evaluates to a symbol. c must be available in the scope where the macro is called.

Examples

julia> t = table((a = [1,2,3], b = ["x","y","z"]));

julia> @transform t {:a + 1}
Table with 3 rows, 3 columns:
a  b    a + 1
──────────────
1  "x"  2
2  "y"  3
3  "z"  4
source

Select data

@where(d, x)

Apply the expression x row by row in d: collect the result as an Array (elements returned by x must be booleans) and use it to get a view of d. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a).

Use {} syntax for automatically named NamedTuples. Use cols(c) to refer to field c where c is a variable that evaluates to a symbol. c must be available in the scope where the macro is called.

Examples

julia> t = table((a = [1,2,3], b = ["x","y","z"]));

julia> @where t :a <= 2
Table with 2 rows, 2 columns:
a  b
──────
1  "x"
2  "y"
source

@filter(d, x)

Filter rows according to the expression x row by row in d. Symbols refer to fields of the row. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a). Use cols(c) to refer to field c where c is a variable that evaluates to a symbol. c must be available in the scope where the macro is called.

Examples

julia> t = table((a = [1,2,3], b=[2,3,4]));

julia> @filter t :a < 3
Table with 2 rows, 2 columns:
a  b
────
1  2
2  3

julia> @filter t 2*:a > :b
Table with 2 rows, 2 columns:
a  b
────
2  3
3  4
source