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

Added getindex(t::Associative, keys...) #4803

Closed
wants to merge 1 commit into from
Closed

Conversation

kmsquire
Copy link
Member

  • convenience function for [getindex(d, key) for key in keys]

Since multidimensional dictionaries seem unlikely, this seems okay to me, but others may reasonably feel that the comprehension syntax is sufficient.

See https://groups.google.com/forum/#!topic/julia-users/5bi3jgJgF8g for the discussion that prompted this.

julia> mydict = ["A"=>3, "B"=>5, "C"=>1]
["B"=>5,"C"=>1,"A"=>3]

julia> mydict[["B","C"]...]
2-element Array{Int64,1}:
 5
 1

julia> c = ["B","C"]; mydict[c...]
2-element Array{Int64,1}:
 5
 1

* convenience function for [getindex(d, key) for key in keys]
@JeffBezanson
Copy link
Member

I think this is too random. Nothing about passing multiple arguments
implies constructing an array. However when you do want this behavior, the
resulting syntax is certainly nice.
On Nov 13, 2013 2:39 PM, "Kevin Squire" [email protected] wrote:

  • convenience function for [getindex(d, key) for key in keys]

Since multidimensional dictionaries seem unlikely, this seems okay to me,
but others may reasonably feel that the comprehension syntax is sufficient.

See https://groups.google.com/forum/#!topic/julia-users/5bi3jgJgF8g for

the discussion that prompted this.

You can merge this Pull Request by running

git pull https://github.com/JuliaLang/julia kms/dict_slices

Or view, comment on, or merge it at:

#4803
Commit Summary

  • Added getindex(t::Associative, keys...)

File Changes

Patch Links:

@kmsquire
Copy link
Member Author

@JeffBezanson, I'm confused... it sounds like you don't like it, but you like it... ;-)

I added an example here. I can see @JeffBezanson's point that there is no precedence for multiple indices producing an array. I still feel that this is reasonable behavior for dictionaries, but I'm good with whatever.

@ivarne
Copy link
Member

ivarne commented Nov 13, 2013

This is the kind of feature where I think it is better to say no than to say yes, because it can be added at any time later, but it can't be removed.

It is also a feature most developers will not know about and I think it has a low guessability factor, so programmers will spend time figuring out what happens when they read other peoples code. The comprehension syntax is not many characters more to write, and if someone finds it unfamiliar they will get more back for their time trying to understand comprehensions.

@StefanKarpinski
Copy link
Member

Yeah, I'm afraid I'm not a fan – I don't even really like the resulting syntax all that much – I prefer the comprehension.

@kmsquire
Copy link
Member Author

Okay, no worries.

@kmsquire kmsquire deleted the kms/dict_slices branch November 14, 2013 06:16
@lejon
Copy link

lejon commented Nov 14, 2013

Hi,

I agree that the syntax resulting from that solution isn't ideal but I do like the functionality, and my experience is that it quite useful in text processing applications. I will admit that I am biased since my "go to" language is Perl, but I see that Julia would be very nice to use in a lot of text applications built around Topic Modeling which is computationally intensive, especially the MCMC implementations.

I do appreciate that one wants to be very conservative in introducing special syntax, but perhaps it could be motivated for such a fundamental data type as Dictionaries.

Best Regards
-Leif Jonsson

@timholy
Copy link
Member

timholy commented Nov 14, 2013

Just to make sure there's no confusion: you already have the functionality available, through the comprehension syntax.

@lejon
Copy link

lejon commented Nov 14, 2013

@timholy: Yep, I'm with you on that! :) I'd just like as nice compact syntax for Dictionary slices as for Array slices, and I also like the symmetry of both Arrays and Dictionaries having a slice syntax. :)

For sure, it is not something that will make or break Julia, but it is that type of convenience that at least I have come to expect of new dynamic languages.

Cheers!
-Leif

@stevengj
Copy link
Member

Sorry I'm late to this discussion, but I would tend to prefer that

mydict[key1, key2, key3]

be equivalent to

mydict[(key1, key2, key3)]

which would give a nice syntax for the common case of dictionaries keyed by tuples of values.

To put it another way, I think mydict[key1, key2, key3] should be conceptually similar to myarray[i1,i2,i3]: it should be a multi-index lookup.

@StefanKarpinski
Copy link
Member

big +1 to @stevengj's proposal.

@nalimilan
Copy link
Member

Does that imply that mydict[[key1, key2, key3]] would return a three-element array? Sounds like a good and consistent solution.

@JeffBezanson
Copy link
Member

No, that will return whatever is in the dictionary.

@JeffBezanson
Copy link
Member

I agree that @stevengj 's interpretation of dict[k1, k2] is the right one, if we want to support that syntax.

@StefanKarpinski
Copy link
Member

No, not at all. That would still return the value associated with the array object [key1,key2,key3]. The only thing @stevengj's proposal does is make mydict[key1,key2,key3] – which is currently a no method error – be syntax for mydict[(key1,key2,key3)], which returns the value associated with the tuple object (key1,key2,key3).

@kmsquire
Copy link
Member Author

FWIW, @stevengj's proposal is Python's behavior.

@StefanKarpinski
Copy link
Member

That is usually an indication that it's a pretty sane choice. In this case, I didn't really need that indication though. As @JeffBezanson said, it's clearly the right behavior.

@nalimilan
Copy link
Member

OK, makes sense, but it's still slightly annoying that you need to use the comprehension syntax to extract several elements. Not a big deal of course.

@lejon
Copy link

lejon commented Nov 20, 2013

So, what will that look like in terms of usage patterns? Is there a reasonably terse syntax and efficient way in terms of performance to transform an array to tuples? I assume that most will have the dictionary keys in an array...

@stevengj
Copy link
Member

@lejon, if you have an array K of keys and a dictionary D, you can just do [D[k] for k in K] to get the "slice".

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.

8 participants