Skip to content

Commit

Permalink
add method of map_asis to dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
vituri committed May 1, 2024
1 parent ad0fc3b commit cabead4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 27 deletions.
14 changes: 11 additions & 3 deletions src/TidierIteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ export
include("adverbs.jl");
export
compose,
compose_n,
negate,
possibly;

include("flatten.jl");
export
flatten_dict,
flatten_dicts_to_df

flatten,
flatten_n,
flatten_dfr,
flatten_json;

include("map dicts.jl");
export
map_asis,
imap_asis;

end #module
2 changes: 1 addition & 1 deletion src/basic map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Apply function `f` to the list `x` and convert each element with function `T`.
"""
function map_type(x, f, T)
map(f, x) .|> T
map_asis(f, x) .|> T
end

"""
Expand Down
39 changes: 16 additions & 23 deletions src/flatten.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using DataFrames, Chain

concat_underscore(a, b) = string(a) * "_" * string(b)

"""
flatten_dict(key, value)
Transform a pair `key` and `value` into a dictionary.
"""
function flatten_dict(key, value)
function flatten(key, value)
Dict(key => value)
end

Expand All @@ -21,7 +19,7 @@ become only a dictionary of values.
Thus, we are "flattening" the inner dictionaries.
"""
function flatten_dict(key, value:: Dict{<:Any, <:Any})
function flatten(key, value:: Dict{<:Any, <:Any})
v = [
Dict(concat_underscore(key, x.first) => x.second) for x value
]
Expand All @@ -34,43 +32,38 @@ end
Remove one layer of dictionaries of a dictionary.
"""
function flatten_dict(dict::Dict{<:Any, <:Any}, n = 1; lists_to_json = true)
function flatten(dict::Dict{<:Any, <:Any}; n = 1)
v = [
compose_n(flatten_dict, n)(x.first, x.second) for x dict
compose_n(flatten, n)(x.first, x.second) for x dict
]

d = merge(v...)

return d
end

flatten_n(n::Int) = compose_n(flatten, n)

"""
flatten_dicts_to_df(dicts::Vector{<:Dict}, n::Int = 1)
flatten_dfr(dicts::Vector{<:Dict}, n::Int = 1)
Given a vector of dictionaries, flatten each of them
and concatenate on a dataframe.
"""
function flatten_dicts_to_df(dicts, n::Int = 1)
function flatten_dfr(dicts; n::Int = 1)
@chain dicts begin
@. flatten_dict(_, n)
@. to_json
@. flatten_n(n)(_)
@. flatten_json
@. DataFrame
vcat(_..., cols=:union)
end
end

function collapse_json(x)
x
end

function collapse_json(x::Dict{<:Any, <:Any})
x |> JSON3.write |> string
end

function to_json(d::Dict{<:Any, <:Any})
for k keys(d)
d[k] = collapse_json(d[k])
end
json_string(x) = x |> JSON3.write |> string
to_json(x) = x
to_json(x::Dict) = x |> json_string
to_json(x::Vector) = x |> json_string

d
function flatten_json(d)
map_asis(d, to_json)
end
14 changes: 14 additions & 0 deletions src/map dicts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function map_asis(d::Dict, f)

v = [Dict(k => f(d[k])) for k keys(d)]

merge(v...)

end

function imap_asis(d::Dict, f)

v = [Dict(k => f(k, d[k])) for k keys(d)]

merge(v...)
end
62 changes: 62 additions & 0 deletions src/test map dicts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using TidierIteration
d = Dict(1 => 1, 2 => 2, 3 => 3)

fff(x) = 2x

@chain begin
d
@aside @show _
map_asis(fff)
@aside @show _
map_asis(x -> x - 1)
end

@chain begin
d
@aside @show _
map_asis(hash)
end

d = Dict(
"a" => 1
,"b" => Dict(
"b1" => 1
,"b2" => 2
)
)

@chain begin
d
# flatten_dict
flatten_dict_to_json
end

d2 = [d, d]

flatten_dfr(d2, n = 2)

dicts = [
Dict(
"title" => "A vida e as opiniões do cavalheiro Tristram Shandy"
,"year" => 2022
,"author" => Dict(
"first_name" => "Laurence"
,"last_name" => "Sterne"
)
)

,Dict(
"title" => "Viagem a roda do meu quarto"
,"year" => 1997
,"author" => Dict(
"first_name" => "Xavier"
,"last_name" => "Maistre"
,"middle_name" => "De"
)
,"editions" => [1997, 1999, 2002]
)
]

flatten_dfr(dicts, n = 0)
flatten_dfr(dicts, n = 1)
flatten_dfr(dicts, n = 2)

0 comments on commit cabead4

Please sign in to comment.