5 ms·
PHP's associative array is very similar yea?
by drbojingle 3y ago
PHP's associative array is very similar yea?
- software_writer 3y agoYes, hashes in Ruby, associative arrays in PHP, maps in Go[1], dictionaries in Python[2] and C#[3] represent the same concept, a collection of key-value pairs. [1]: https://gobyexample.com/maps https://gobyexample.com/maps [2]: https://docs.python.org/3/tutorial/datastructures.html#dictionaries https://docs.python.org/3/tutorial/datastructures.html#dicti... [3]: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-7.0 https://learn.microsoft.com/en-us/dotnet/api/system.collecti...
- my_new_account0 3y agoYes. The OP's post is basically "baby's first API." The are a very handy API but relying on them at scale is much less efficient than structs or arrays. This post doesn't even go into the different performance characters of these APIs, which can be a major foot gun. I've noticed a lot of novice-level posts on here lately. Although I admit the post was very well written.
- ceautery 3y agoNovice level posts are great. Someone found something exciting to them, not new to us, but we should encourage them to keep exploring and writing.
- schrodinger 3y agoAlso, the votes imply interest!
- dikei 3y agoI find it funny that he said he didn't know what to pick between 5 types of IDictionary in C#. * Hashtable * SortedList * SortedList<TKey, TValue> * Dictionary<TKey, TValue> * ConcurrentDictionary<TKey, TValue> I don't even know C# (just Java), yet I know that you'd probably want to use Dictionary<TKey, TValue> most of the time.
- software_writer 3y agoTo be honest, I wrote that just to add a little drama ;)
- djur 3y agoMy experience is that Struct only performs better than Hash in a pretty narrow set of circumstances, rarely enough to be a deciding factor unless profiling suggests it to be.
- Glyptodon 3y agoRuby structs or c-family structs?
- crote 3y agoThe interesting thing is that it definitely feels different to me. While the basic data structure is obviously identical to its counterparts, the way it interacts with the ecosystem as a whole makes it feel way more powerful. The interaction of hashes, symbols, and blocks lead to a language which feels like it is designed with DSLs as first-class citizens. This leads to things like Ruby on Rails, which at first glance feels like pure magic. I get a somewhat similar feeling about lists in Python. Because of its very extensive list comprehension you start thinking in lists, leading to some very clean code which would be extremely awkward to implement in many other languages.
- software_writer 3y ago> The interaction of hashes, symbols, and blocks lead to a language which feels like it is designed with DSLs as first-class citizens. Totally agree.
- johannes1234321 3y agoSelling it as associative array is nice. (And I know the docs do that, too) It's a doubly linked list, array, hashmap all in one, with some weird magic when handling numeric indices. There's often the time when using another language that I made a bad choice by picking either an array or hash or list while later I need one of the other properties PHP would give me by default. The extra cost for most uses is neglectible.
- tialaramex 3y ago> It's a doubly linked list, array, hashmap all in one, with some weird magic when handling numeric indices. In what way, exactly, is PHP's associative array (a hash table of course, like all these things) a doubly linked list ? Sure, in a language which isn't very fast anyway, I can believe it's reasonable to use your hashtable as an array, O(1)~ or O(1)* [Expected or Amortized constant time] probably feels enough like 1 [a single operation] in this language that you shouldn't worry about it. Explaining to beginners why they can't write digits["four"] is more trouble than it's worth, so how about just make it work instead. It is a hashmap, so, that makes sense. These are different words for essentially the same type. But how is it a doubly linked list ?
- johannes1234321 3y agoSeems to have changed in the implementation indeed and I now didn't spend much time thinking through the current implementation. But the old implementation had pointers to next and last (previous) element in the list: https://github.com/php/php-src/blob/PHP-5.3/Zend/zend_hash.h#L59 https://github.com/php/php-src/blob/PHP-5.3/Zend/zend_hash.h...
- tialaramex 3y agoOh I see. Yes, this is a very common way for C programmers - in particular - to make a hash table, it's the design that C++ enshrines as std::unordered_map and it's the data structure you'd have been taught in a University data structures course in the 1980s, and maybe even the 1990s [depending on who is teaching and where] While internally this is indeed using linked lists, I don't think you can make use of that from PHP which is what I'd imagined was meant. Suppose I had a million integers that I found somewhere, and then I want to splice in a different million integers I have, so that in total there are two million integers. With linked lists that's very cheap, basically instant, but with most data structure, including a hash table, not so much. Linked Lists are very rarely what you needed, even though when they are what you needed they're invaluable. They are really easy to implement in C, so it's sort of a "I've got a hammer, now all I see are nails" situation in C.