News for hackers. Stuff that matters.
Re:Lisp-Stick on a Python by mtreis86 on Tuesday November 15, 2022 @10:20AM (#33610178)
I have no idea what you can or can't easily do in Rust. Here is something many languages can't do so succinctly, without closures. 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

There are no comments on this comment yet.