3 ms·
Here's a more or less equivalent implementation in Common Lisp, using alist-hash-table from alexandria: (defun csv-to-maps (csv) (loop for row in (re
by jlarocco 6y ago
Here's a more or less equivalent implementation in Common Lisp, using alist-hash-table from alexandria:
(defun csv-to-maps (csv)
(loop for row in (rest csv) collecting
(alist-hash-table
(mapcar #'cons (first csv) row) :test #'equal)))
For CSVs with only a few columns (maybe less than 10?), I'd probably go with alists instead of a hash table to save memory, and it'd look like this, using curry from Alexandria:
(defun csv-to-alists (csv)
(map 'list (curry #'mapcar #'cons (first csv)) (rest csv)))
- didibus 6y agoIf you read Rich's paper, there's a section where it says: > maps were built behind several abstractions. Thus, maps can start with a representation optimized for small maps and switch to a representation optimized for large collections if and when they ‘grow’ beyond a certain size. Which is pretty cool, because basically the Clojure version of your code abstracts over alist-hash-table and alist, and the most optimal type of concrete map will automatically be created based on the size of the map you are asking to create.
- jlarocco 6y agoThere's actually an implementation of the same HAMT data structure that Clojure uses available in QuickLisp and on GitHub: https://github.com/danshapero/cl-hamt/ https://github.com/danshapero/cl-hamt/ Technically, since the hash-table implementation isn't specified in the CL standard an implementation could use a HAMT for the built-in hash tables, but I don't think any do.
- didibus 6y agoThe construction of a Map is abstract in Clojure. The concrete type of Map you get is not something you specify. This isn't related to the HAMT per say. So what I mean is Clojure will use an array backed map when you ask it to construct a small map, and it will give you a HAMT backed map when the map is large. This is true as well as you add elements to a map, Clojure might automatically promote the map from an array map to a HAMT as its size grows.