-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make keys(::Tuple)
return a Tuple
.
#24897
Conversation
Wouldn't you want |
That is the current behavior - personally I don't find that output type particularly useful since it hides the type of where the keys came from (one assumes an array or something), their length is no longer statically known, etc. |
I guess one could have a "static" version of |
This is definitely a feature. Getting the same answer from multiple places is always preferable.
This is false. The same information in contained in |
Hmm... so the types of situations I'm considering is when you have some data in some indexable container map(f1, a)
map(f2, keys(a))
map(k -> f3(k, a[k]), keys(a)) # could also be using `pairs`
map(f4, pairs(a))
map(f5, values(a)) It would be nice if you tended to get the same type of container back. The idea is to build a coherent interface that works across indexable containers in Julia - specifically |
Allow me to translate: their length is no longer part of their type. Which is true. |
Another question: What will keys for named tuples return? |
To fulfill what I said above about In this picture you end up with key containers which are idempotent under indexing ( For named tuples I’d return a similar container (named tuple) with same keys and keys as values. Thus I propose: keys((a=1, b=2.0)) === (a = :a, b = :b) I get that the answers are sometimes a bit surprising, but I feel the logic is sound and being able to |
@andyferris I've been following your developments along these lines with great interest. Are there are any examples of the expressive improvements enabled by a consistent indexing scheme that you could point me to? There's been a lot of discussion in various issues and it's sometimes been a little bit difficult to follow from the sidelines, so it's quite possible that I just missed the right thread. As a side note, Mathematica has a Dataset type that with a lot of powerful functionality packed into a set of indexing primitives: http://reference.wolfram.com/language/ref/Dataset.html The precise mechanics there are sometimes rather arcane (some functions get applied on the way down to filter or select, and others get applied on the way up, to aggregate) but it feels like this is playing in the same space as your recent push towards a consistent basic interface. |
@yurivish Thanks for following along. It seems like Mathematic Datasets are basically tables / dataframes. I'll try get back with some more broadly motivating examples, but the kinds of things I've been trying to address in the last while are highlighted in my new prototype Indexing.jl, in particular the usage of |
Could I ask triage to please consider whether this is desired for v1.0 - in particular the broader issue of having the various incantations of (My position is that this is the most useful, convenient and consistent way of mapping/iterating collections of data for "data science" type applications, and having such APIs be consistent in their semantics across arrays, dictionaries, and (named) tuples would be a great boon). |
I would hold off on this until/unless it becomes clear that we need it. In particular it's not clear whether this applies to dictionaries. |
See my comment on #25013 (comment). |
This seems the most sensible choice to me, so that
keys((1, 2.0, :c)) === (1, 2, 3)
.I will find this useful where I tend to use things like
map
onkeys
and it's really, really nice to preserve the properties of the container when you do this. Mapping keys of a tuple might also be a nicer replacement for some particular usages ofntuple
. (Usemap(f, keys(t))
instead ofntuple(f, Val(length(t)))
).Relatedly, I've been thinking about generalizing
getindex
for getting multiple elements out of dictionaries (#24019) and other containers including tuples, and it seems the easiest definition goes like:where I feel you also want something like this:
which is provided by this PR.