Flatten.jl icon indicating copy to clipboard operation
Flatten.jl copied to clipboard

Retain keys for associatives

Open rofinn opened this issue 4 years ago • 3 comments
trafficstars

We have some similar code for flattening associative types, but we typically want to retain and join the key information. Would there be any interest in merging our features into one API? I'm happy to start a PR, but maybe you think that's out of scope for this package?

julia> using Flatten, AxisSets

julia> Flatten.flatten((a = 1, b = (c = 2, d = 3)))
(1, 2, 3)

julia> AxisSets.flatten((a = 1, b = (c = 2, d = 3)), :_)
(a = 1, b_c = 2, b_d = 3)

https://github.com/invenia/AxisSets.jl/blob/main/src/flatten.jl

rofinn avatar Mar 18 '21 16:03 rofinn

Thats a good solution to duplicate keys.

I would be happy with a PR, with a few caveats:

  • Mostly flatten compiles away and has no/little runtime cost - this PR shouldn't affect that.

  • you will probably need to make sure you pass the previous key argument at compile time, so in Val or StaticSymbol, or make sure constant prop works.

  • The code here is pretty hard to understand! (recursive generated functions written by rdeits and adapted in my first year using Julia ;) Im thinking of rewriting Flatten.jl as ObjectQueries.jl to also have lens compatability with Accessors.jl, I just havent had time. But this functionality could roll over into that too.

rafaqz avatar Mar 18 '21 22:03 rafaqz

Cool, I'll work on a PR when I have some time.

Mostly flatten compiles away and has no/little runtime cost - this PR shouldn't affect that.

Hmm, okay, I think I can do that for some types, but not all? Would it be acceptable to say that this feature wouldn't impact the current API (methods) or its performance guarantees? I'm just not quite sure how I'd achieve that for Dict or iterables of Pairs.

rofinn avatar Mar 21 '21 19:03 rofinn

Flatten.jl doesnt flatten Dict or Array or anything with with variable length at runtime as it breaks the symmetry of reconstruct. You can of course Flatten to whole Dict and do things manually. But the idea would be to flatten/reconstruct a NamedTuple instead.

Also Dict is rarely a useful container for me in a context I need to flatten, but I can see how it could be. Maybe your naming scheme would actually solve the reconstruct issue.

rafaqz avatar Mar 21 '21 21:03 rafaqz