4 ms·
I was looking for reader macros for recognizing {} as hash tables and [] as vectors. To me, that is one of the biggest innovations in Clojure over Lisps that c
by jimbokun 16y ago
I was looking for reader macros for recognizing {} as hash tables and [] as vectors. To me, that is one of the biggest innovations in Clojure over Lisps that came before, the expansion of the concept of S-expressions.
- francoisdevlin 16y agoPersonally, my money is on the seq abstraction. YMMV.
- jimbokun 16y agoThat is also an excellent choice. But I believe they are related, or at least complement each other, in the sense that all of the data structure literals implement seq. I do not think they would work as well as an extension for S-expressions, otherwise.
- Xurinos 16y agoI am not a CL expert, but I was able to whip this up in short time (and I sacrificed a little readability for brevity): (defun set-hash-values (hash pairs) (when pairs (setf (gethash (car pairs) hash) (cadr pairs)) (set-hash-values hash (cddr pairs)))) (defun in-place-hash (stream char) (declare (ignore char)) `(let ((hash (make-hash-table))) (set-hash-values hash ',(read-delimited-list #\} stream t)) hash)) (set-macro-character #\{ #'in-place-hash) (set-syntax-from-char #\} #\)) But to be honest, I think this {} stuff is an abuse of the hash structure. People are really looking to make quick anonymous classes/structs; you see this all the time in the perl world, and now we get to see this abuse promoted and propagated in Clojure. In that case, {} should really translate to a MOP-generated anonymous class. Also, you are stuck with the decision some developer made for your hash's efficiency trade-offs. In my reader macro, I left things at make-hash-table defaults, but that might not be appropriate for your situation. Do you want to be able to read quickly from your hash in exchange for slow writes, or do you want to be able to write quickly to the hash in exchange for slow reads? On another note, the [] vector reader macro is much easier to create. I just wanted to prove to myself that I could write {}. But this additional syntax is annoying. In practice when programming Lisp, it is easy to slap in a bunch of ))))) until the right alternate parentheses highlights. Now I have to remember to slap in ] and } at the right spots, leaving me with )]}]]}})) noise. Maybe that is why CL's in-place vector syntax is #(). Anyway, I would hesitate to call {} and [] an innovation.
- swannodette 16y ago> Also, you are stuck with the decision some developer made for your hash's efficiency trade-offs. PersistentHashMaps are not slow. If you think they are, you can provide a custom defrecord with the same fields to the functions of the API and get the all perf benefits without breakage. > leaving me with )]}]]}})) noise Guess you never heard of paredit. > Anyway, I would hesitate to call {} and [] an innovation. Whatever floats your boat man.
- fogus 16y ago"People are really looking to make quick anonymous classes/structs" What wrong with that? If you have the need for an anonymous map, then you use a map. If you have the need for a named map then you use a record. Clojure gives you both options. "Also, you are stuck with the decision some developer made for your hash's efficiency trade-offs." In that case you just use a different type of hash map. In Clojure you have the choice of the literal syntax (which by the way manages efficiency concerns on your behalf), the hash-map, array-map, or sorted-map functions, or records, or Java maps for interop, or you can implement your own if none of those options work.
- Xurinos 16y ago> What wrong with that? If you mean, "What is wrong with anonymous classes?", I have no answer. I do not mind the idea, so long as I can maintain the integrity of my data's interfaces. If you mean, "What is wrong with implementing anonymous classes as hash maps?", then I point you towards issues of efficiency in memory and speed. Hash maps are intended to make key look-up quick for a variable set of not 100% known keys. If all you want is known key-value pairs, then you have some options that do the trick much more efficiently (examples --): - structs (key is compiled out to be a quick pointer addition) - classes (on top of the struct, you now have some abstraction capabilities that will help out future maintenance issues) If you do not quite know what you want in the structure, but you have a small number of keys, an associative array with a decent accessor API is all you need. The few key comparisons will generally be faster than running them through a typical hashing algorithm. When you provide a construct like this: { :foo 1 :bar 2 } you are implying that you have a known set of keys and values. With a hash map, I would expect that someone would later add more key-value pairs, but in practice, code that starts with this initialization pattern treats it like a class rather than a hash, just modifying the values of the :foo and :bar attributes. I have also seen instances where developers would -- because it was easy to do with these hash maps -- slap in another key-value pair in some subfunction and then suffer interface issues down the road when they assume its presence in the wrong places. Something a little stricter would have at least given them a warning. So just make it a struct -- anonymous if you need -- at minimum. That is the most generic, all-purpose form that could benefit from this kind of {} syntax. > which by the way manages efficiency concerns on your behalf Can you elaborate on this? If this means that there is a magical conversion to a stricter struct underneath the hood, then I have no place to complain about {} in Clojure's case. I have not encountered this notion before (other than as faith).