4 ms·
Python has https://docs.python.org/2/library/collections.html#collections.OrderedDict https://docs.python.org/2/library/collections.html#collectio... I've neve
by anonymoushn 12y ago
Python has https://docs.python.org/2/library/collections.html#collections.OrderedDict https://docs.python.org/2/library/collections.html#collectio...
I've never wanted this though. I just discovered OrderedDict when I was looking for something like std::map.
- skrebbel 12y agoCool! I didn't know that!
- pornel 12y ago> I've never wanted this though. When it's not the default normal thing, you don't build solutions around it, so you never see what you're missing :) In PHP I make lots of tiny uses of it in many places. I really missed it when I switched to Python (sure, there's OrderedDict, but it's a second-class citizen: there's no syntax for literals and standard APIs don't explicitly take advantage of it). * It's very useful for deduplicating things without losing order (especially when you have a bigger algorithm that collects data from multiple sources or a tree structure into one array). * It's neat for sorting objects without having to mutate them to add a key or wrap them in a key/value object. * It's great for configuration with JSON-like structures, but key order gives extra flexibility in the design, e.g. instead of [{id:"foo"},{id:"bar"}] you can use ["foo"=>[],"bar"=>[]]. It's not a major feature, but it makes things nicer. I've never had arrays accidentally randomized in PHP, but had bugs due to careless list->dict->list conversions in Python.
- yedpodtrzitko 12y agoWhat I read is "if all you have is a hammer, everything looks like a nail." > In PHP I make lots of tiny uses of it in many places. I really missed it when I switched to Python (sure, there's OrderedDict, but it's a second-class citizen: there's no syntax for literals and standard APIs don't explicitly take advantage of it). Missing syntax sugar makes it a second-class citizien? How? Also what advantages could standard API (<- what does that even mean?) take? > It's neat for sorting objects without having to mutate them to add a key or wrap them in a key/value object. It's called set() > It's great for configuration with JSON-like structures, but key order gives extra flexibility in the design, e.g. instead of [{id:"foo"},{id:"bar"}] you can use ["foo"=>[],"bar"=>[]]. ...and the reason you cant use OrderedDict here is you dont like it. About ordering stuff: after years I can still remember the problems I had with ordering in PHP. There's more than a dozen of sorting methods, which is mess. No one can remember if they all behave the same way and what's the order of its parameters. In Python you've sorted() and that's it. You cant do any advanced stuff with these array, because sometime they act as lists and sometime they act as hashmaps. I'll take this example from "Fractal...": $first = array("foo" => 123, "bar" => 456); $second = array("foo" => 456, "bar" => 123); array_diff($first, $second);