3 ms·
Four Reasons to Use Maps Instead of Classes in Clojure
- lokedhs 11y agoI'm not against the idea of using maps for (almost) everything. It's definitely convenient and something I do in Clojurescript. That said, I mostly use Common Lisp, and I really enjoy CLOS. As far as I can tell none of his examples would be any more complex in CL if the objects were CLOS instances. It's definitely safer to use a slot reader in CLOS than the keyword functions that look up a value by key in Clojure. This is not helped by the fact that :foo returns nil instead of throwing an error if the key :foo doesn't exist in the map.
- lispm 11y agoBasically that's what Lisp did with assoc-lists and property-lists since the dawn of time. CL-USER 63 > (pprint *players*) #((:NAME (:FIRST "Jose" :LAST "Peraza") :STATS (:STOLEN-BASES 35 :HOME-RUNS 6) :POSITION :SS) (:NAME (:FIRST "Mike" :LAST "Trout") :STATS (:STOLEN-BASES 15 :HOME-RUNS 36) :POSITION :OF) (:NAME (:FIRST "Manny" :LAST "Machado") :STATS (:STOLEN-BASES 20 :HOME-RUNS 30) :POSITION :SS)) CL-USER 64 > (elt *players* 0) (:NAME (:FIRST "Jose" :LAST "Peraza") :STATS (:STOLEN-BASES 35 :HOME-RUNS 6) :POSITION :SS) CL-USER 65 > (getf * :position) :SS CL-USER 66 > (loop for p across *players* sum (getf (getf p :stats) :home-runs)) 72 Later various dynamic object systems have been written for Lisp, for example the Common Lisp Object System (CLOS). 'Dynamic' means that they don't have the drawbacks of the static object systems, but still provide many of the advantages of OOP.