I use PHP a fair amount, but this is one area that irks me. PHP arrays can be arrays, lists, and hash maps. PHP obviously copied some of Perl's features, and I never understood why hashmaps and arrays weren't different types, as they are in Perl.
I use PHP a fair amount, but this is one area that irks me. PHP arrays can be arrays, lists, and hash maps. PHP obviously copied some of Perl's features, and I never understood why hashmaps and arrays weren't different types, as they are in Perl.
Thanks to their mixed nature they're very versatile. People even make small DSLs out of them. And they're order-preserving, so they won't inject non-determinism just to smugly teach you a lesson about real hash tables.
If a programmer use lists in lisps where vectors or hash tables are more appropriate, he is doing something wrong.
Likewise, simulating arrays in hash tables, which is what PHP expects one to do, is a very bad idea.
Bash lacks multidimensional arrays, but it does have hash tables, so some programmers resort to simulating multidimensional arrays by using keys such as `"3,4"` as a string in hash tables, this would be a very bad idea if Bash had true multidimensional arrays. In it's lack thereof, it is only a bad idea where no better idea is possible.
If you do need to deal with data structures that are quite large (i.e., take GBs of memory), perhaps PHP is not the right language.
This is the TXR Lisp interactive listener of TXR 249.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
Poke a few holes in TXR with a fork before heating in the microwave.
1> [mapcar succ "abc"]
"bcd"
2> (car "abc")
#\a
3> (cdr "abc")
"bc"
4> (cadr "abc")
#\b
5> (cddr "abc")
"c"
6> (cdddr "abc")
nil
7> (rplaca (copy "abc") #\x)
"xbc"
8> (rplacd (copy "abc") "yz")
"ayz"
9> (ldiff "abcd" "cd")
"ab"It also makes some potential niceties impossible. Python can give a special meaning to a negative index into a list, because it can never truly exist, but PHP has to treat it like a key and do a normal lookup.
In fact, they're getting slower in real practice. (We've been trading off speed for safety and power consumption for years now.)
Of course there is Ds\Sequence, but having it as a part of some relatively obscure library isn't the same as having it as a part of a language, with syntax as simple as [1,2,3]. Actually, I'm not even sure: does it have as good performance, as "arrays" do? I never tested.
(I was involved in the development of this function: https://github.com/php/php-src/pull/4886)
As far as PHP is concerned, a list is a subset of arrays where the keys are ordered integers starting at zero. Any array that doesn't fit this pattern could be considered a "map" -- there's no middle ground.
Name your vars $some_list for integer arrays and $some_hashmap (sometimes $some_coll (collection)).
I'm 1/2 joking. I maintain loads of legacy and recent PHP LOB apps. Var-name fixes can do a lot to help the case in any loosely typed langs (I learned the trick from VB4.0)