Julia macros to easily construct named tuples and set properties of mutable structures. AddToField.jl exports three macros, @addnt, for constructing NamedTuples, @adddict, for creating Dicts with Symbols as keys, and @addto! for modifiying existing data structures. Github repo here.

To create NamedTuples, use @addnt:

julia> using AddToField;

@add b = a + 2
end
(a = 1, b = 3)

@add "Variable a" a = 1
@add b = a + 2
end
(Variable a = 1, b = 3)

To create Dicts, use @adddict:

julia> using AddToField;

@add b = a + 2
end
Dict{Symbol, Int64} with 2 entries:
:a => 1
:b => 3

@add "Variable a" a = 1
@add b = a + 2
end
Dict{Symbol, Int64} with 2 entries:
Symbol("Variable a") => 1
:b                   => 3

To modify existing structures, use @addto!

julia> D = Dict();

@add b = a + 2
@add "Variable c" c = b + 3
end
Dict{Any,Any} with 3 entries:
:a                   => 1
:b                   => 3
Symbol("Variable c") => 6

AddToField makes working with DataFrames easier. First, makes the creation of publication-quality tables easier.

using DataFrames, PrettyTables, Chain
julia> df = DataFrame(
group = repeat(1:2, 50),
income_reported = rand(100),
income_imputed = rand(100));

julia> @chain df begin
groupby(:group)
combine(_; keepkeys = false) do d
@add "Mean reported income" m_reported = mean(d.income_reported)
@add "Mean imputed income" m_imputed = mean(d.income_imputed)
end
end
end
┌───────┬──────────────────────┬─────────────────────┬────────────┐
│ Group │ Mean reported income │ Mean imputed income │ Difference │
├───────┼──────────────────────┼─────────────────────┼────────────┤
│     1 │             0.523069 │             0.53696 │ -0.0138915 │
│     2 │             0.473178 │             0.41845 │  0.0547277 │
└───────┴──────────────────────┴─────────────────────┴────────────┘

It also makes constructing data frames easier

julia> using DataFrames

x = ["a", "b", "c"]
@add x = x .* "_x"
@add x_y = x .* "_y"
end
3×2 DataFrame
Row │ x       x_y
│ String  String
─────┼────────────────
1 │ a_x     a_x_y
2 │ b_x     b_x_y
3 │ c_x     c_x_y

Note

You cannot use @add in new scopes created with body. The following will fail

@addnt begin
let
a = 1
end
This is because @addnt, @adddict, and @addto! create anonymous variables, then constructs and modifies objects at the end of the block. The same applies for for loops and functions inside the @addnt, @adddict, and @addto! blocks. In theory, @addto! should not have this limitation. However I implementing this feature in @addnt is more complicated, and at the moment maintaining simple feature parity is important.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5358376,"math_prob":0.93131506,"size":2759,"snap":"2022-27-2022-33","text_gpt3_token_len":938,"char_repetition_ratio":0.17604356,"word_repetition_ratio":0.13963965,"special_character_ratio":0.29394707,"punctuation_ratio":0.15670103,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9913153,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-06T07:14:58Z\",\"WARC-Record-ID\":\"<urn:uuid:fcd56c2f-e2fe-41d3-9b07-f4803ac230c2>\",\"Content-Length\":\"10272\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8afc6edd-5506-4200-b3cf-3e7d293e692b>\",\"WARC-Concurrent-To\":\"<urn:uuid:7031107b-a70d-4b2a-bf3b-85a93543be9e>\",\"WARC-IP-Address\":\"185.199.110.153\",\"WARC-Target-URI\":\"https://pdeffebach.github.io/AddToField.jl/dev/\",\"WARC-Payload-Digest\":\"sha1:BTU45ZZ3ITABFFUPO65VPOTAPS2UUOTJ\",\"WARC-Block-Digest\":\"sha1:MLQNRM2BGY2ZX7WXBSXKIH7NNRAUFKZC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104668059.88_warc_CC-MAIN-20220706060502-20220706090502-00443.warc.gz\"}"}