Skip to content
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

Deconstructing Named Tuples #47131

Closed
wants to merge 9 commits into from
Closed

Deconstructing Named Tuples #47131

wants to merge 9 commits into from

Conversation

alfaromartino
Copy link

This is my first PR, so not sure if it should be added. here. It's for a feature that could come in handy.

Given a NamedTuple nt = (; a11=1, a12=2, b1=3), we can deconstruct it by using (; a11,a12) = nt . However, you need to specify each variable you want to deconstruct. I think it could be worth adding some way to deconstruct all variables, or to use regular expressions for choosing the variables to deconstruct. Something along the lines (; r"^a") = nt to get a11 and a12 into local scope.

The main advantage of using NamedTuples relative to struct is that it complements perfectly with DataFrames (you can always convert a row of a DataFrame into a NamedTuple by using rowtable).
The typical use I have in mind is for Economics, but it'd be useful for any kind of model. For instance, right now I'm working with a dataframe encompassing hundreds of industries. Since it requires a lot of previous transformations, using DataFrames is a must. But, then, I compute a model that uses information for each industry and makes use of tons of variables for each industry. So, it's easy to convert each industry info into a NamedTuple (instead of working with a struct where you have to manually specify the type, name, etc).
However, you need to manually add the variables that you deconstruct for every function in the model. The same is true for the parameters of the model. Right now, Parameters.jl allows you to deconstruct all variables in a struct, but this is not possible for NamedTuples.

It seems other users also thought this feature could be useful. See here and here.

And from what I understand from #39285, it's a feature that could be added? (@simeonschaub and @CameronBieganek maybe already explored this?)

Thanks!!

c42f and others added 9 commits August 17, 2022 11:41
* Install JuliaSyntax as a stdlib
* Install this as the default parser when the environment variable
  JULIA_USE_NEW_PARSER is set.
This is required because we'd like to set the binding during
Base.__init__. This can be done with `Core.eval` but that doesn't work
well in incremental compilation mode.
Co-authored-by: Shuhei Kadowaki <[email protected]>
@simeonschaub
Copy link
Member

simeonschaub commented Oct 11, 2022

Please use https://discourse.julialang.org for discussions. PRs are meant for contributing code, not for requesting features

@maleadt maleadt deleted the tb/juliasyntax branch October 12, 2022 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants