4 ms·
I have no idea what you can or can't easily do in Rust. Here is something many languages can't do succinctly, without closures and code-as-data. From Paul Graha
by mtreis86 4y ago
I have no idea what you can or can't easily do in Rust. Here is something many languages can't do succinctly, without closures and code-as-data. From Paul Graham's book, On Lisp, modified to work with Lisp code as keys.
(defun make-dbms (db &key (test #'eql))
"Make a database, db should be a list. Test determines what keys match."
;;Three closures in a list to make a database.
(list
#'(lambda (key)
(rest (assoc key db :test test)))
;;add
#'(lambda (key val)
(push (cons key val) db)
key)
;;delete
#'(lambda (key)
(setf db (delete key db :test test :key #'first))
key)))
(defun lookup-dbms (key db)
"Return the value of an entry of db associated with the key."
(funcall (first db) key))
(defun add-dbms (key val db)
"Add a key and value to db."
(funcall (second db) key val))
(defun del-dbms (key db)
(funcall (third db) key))
In use with code as the key:
CL-USER> (let ((db (make-dbms nil :test #'equalp)))
(add-dbms '(+ 1 2) 3 db)
(lookup-dbms '(+ 1 2) db))
=> 3