-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 Hash#first(n : Int) return subhash #2280
Conversation
I'd be curious about the usecase for that, I still consider depending on the order of a map that much a code smell. |
My use case is the follwing. I have a hash where key is a name and value is a rate of entity. |
Why is it a hash, compared to say an array of tuples or structs? |
Well, it can be of course array of tuples, but then again, I will need to convert it back to Hash, because Hash is much easier to work with. I need to get rate(value) by name(key) |
@jhass Actually, does Crystal hash guarantee an order of elements? I've realized, that I am not sure about this. If no, this PR does not make sense and you can close it. |
It does, it maintain a single linked list through the hash items just like Ruby's does. |
I'm not sure there is any guarantee for hash elements to be ordered, I would say this is an implementation detail and shouldn't be relied upon. |
But given there's a first instance method, this is already relied on, isn't it? |
Hash keeps order of insertion, it's not said in the API docs but it should (I didn't have time to document it yet). That said, we can always reconsider this "feature": it takes more memory and logic to maintain the order, but maybe it's not used that much, I don't know. I'm not sure about a |
I am working on a natural language detection tool, based on N-grams: def self.guess_all(sample_text, top = 5)
sample_trigrams = LanguageProfileBuilder.build(sample_text)
dists = Array(Tuple(String, Float64)).new(@@profiles.size)
@@profiles.each do |code, lang_trigrams|
dist = Comparer.distance(sample_trigrams, lang_trigrams)
dists << {code, dist}
end
sorted = dists.sort_by { |kv| kv[1] }
sorted.first(top)
end So, in this example I use
Finally, I want the method to return Hash of first n-top languages with their distances. P.S. It's also would be good to have a proper |
Can't you do |
Well, of course I can, I still I would prefer to have this method on Hash. Maybe, if you don't really like the idea, we can close the PR, until somebody else raises similar quation. I am OK with it. |
My main concern is that I don't know what would be more intuitive: should Basically, all of the above doubts :-) |
@asterite I guess you're right, it's a topic to discuss. Personally for me it would be more natural if it returns hash... But if we take a look at Ruby it returns Array of arrays: {a: 10, b: 20, c: 30}.first(2)
=> [[:a, 10], [:b, 20]] So for Crystal it would be Array of tuples (still it is better then Array of arrays) :). So I you think it will be more efficient to return Array of tuples I am OK with :) |
@greyblake Let's make it return an array of tuples, like in Ruby. Then I'll merge. Thanks! |
OK, thanks, I will, as soon as I solve this problem on my Debian laptop:) #1964 |
Now that |
Cool! Thanks! |
Implement
Hash#first(n : Int)
method to return subhash.E.g.