-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested Tuples #11
Comments
Flatten.jl will handle that (err maybe I misunderstood your use-case). Also have some code here that incorporates lenses and nested tuple flattening into a single process: I need to turn that into a package or integrate it with Accessors.jl at some point, but can't really fid the time right now. |
Thanks a lot for opening this!
Very excited to see Setfield used in this context. I am a probabilistic programming fan boy (by no means an expert). But some time ago, I wondered if Gen.ChoiceMaps could be replaced by something with more optics flavor.
I think Accessors is stable enough so that it makes sense to use it in a new project. I think function lenses are more convenient with Accessors and they might be useful for what you are doing. Also, Accessors allows more general optics than just lenses, which might also be useful.
Probably things can be abstracted differently and some parts moved to Accessors. How and which parts? No idea 😄 I think that needs exploration and I think it's easier if you do it in your own package. If you discover specific pieces that you feel should live in Accessors, please tell us. |
Thanks for the feedback. I'll try to move things over to Accessors and let you know if I hit any snags. |
Yeah, nested ModelParameters.jl is pretty much a wrapper for Flatten.jl for a specific use-case. Eventually it should also be buillt on that code I linked to - either in Accessors.jl or the yet to be written ObjectQueries.jl - because it would be nice to use Optics with the nested flattening for some tasks, like I do in that example. It's just not clear to me how best to integrated these things yet. |
Basically what I want is something like StructArrays, but with stronger support for nested (named) tuples. I'm making some progress! I used some generated function tricks to get this working, think it can become the core of a very efficient julia> using NestedTuples
julia> x = (a=[1,2], b=(c=([3,4], [5,6]),d=[7,8]))
(a = [1, 2], b = (c = ([3, 4], [5, 6]), d = [7, 8]))
julia> @btime leaf_setter($x)(1,2,3,4)
0.016 ns (0 allocations: 0 bytes)
(a = 1, b = (c = (2, 3), d = 4)) |
Ok, I managed to put together a Here's a little example: function ind(x, j)
f(arr) = @inbounds arr[j]
modify(f, x, Leaves())
end Then we can do julia> x = (a = [1, 2], b = (c = ([3, 4], [5, 6]), d = [7, 8]));
julia> @btime ind($x, 1)
1.901 ns (0 allocations: 0 bytes)
(a = 1, b = (c = (3, 5), d = 7)) As one building block for this, I had been trying to do leaves(x, y...) = (leaves(x)..., leaves(y)...)
leaves(x::Tuple) = leaves(x...)
leaves(x::NamedTuple) = leaves(values(x)...)
leaves(x) = (x,) I would expect that to be type-stable, but it's not (try |
Cool! About the |
I could get in inferred by doing the following, but probably very brittle and still worth opening an issue: module M
leaves(x::Tuple) = cattuples(map(leaves, x))
leaves(x::NamedTuple) = cattuples(map(leaves, values(x)))
leaves(x) = (x,)
cattuples(x) = _cattuples(x...)
@inline _cattuples(x, y...) = (_cattuples(x)..., _cattuples(y)...)
@inline _cattuples(x, y) = (_cattuples(x)..., _cattuples(y)...)
@inline _cattuples(x::Tuple) = _cattuples(x...)
@inline _cattuples(x) = (x,)
x = (a = [1, 2], b = (c = ([3, 4], [5, 6]), d = [7, 8]));
using Test
@test leaves(x) == ([1, 2], [3, 4], [5, 6], [7, 8])
@inferred leaves(x)
end |
Good idea
Oh, that's funny. I got rid of the |
I think factoring out _cattuples(x, y) was the crucial part. |
Right, Michael Abbott suggested something similar ("reduce splatting"): |
Hi, I just put together this little proof of concept using Setfield:
https://github.com/cscherrer/NestedTuples.jl
I wonder if it could make more sense to move it to using Accessors. Also, @rafaqz , I just saw your post, looks like there might be some overlap in our needs. Pinging you here in case that's right :)
The problem I'm working on is a good way to represent samples in a Monte Carlo simulation. In Soss, each sample is a named tuple. And I need to manage these programmatically, so I need a clean interface.
I thought StructArrays might already do it, but from what I've seen so far, StructArrays don't have much support for nested structure. So I started down this road today thinking I might get to something more tailored for the problem.
So I guess my questions are
I've been really impressed with the data manipulation work I've seen from you (@jw3126 and @tkf ), so I hope anything I contribute can fit cleanly into that :)
The text was updated successfully, but these errors were encountered: