Advertisement
Guest User

cd database

a guest
Jun 22nd, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.70 KB | None | 0 0
  1. (defvar *db* nil)
  2.  
  3. (defun make-cd (title artist rating ripped)
  4.   (list :title title :artist artist :rating rating :ripped ripped))
  5.  
  6. (defun add-record (cd) (push cd *db*))
  7.  
  8. (defun dump-db ()
  9.   (dolist (cd *db*)
  10.     (format t "~{~a:~10t~a~%~}~%" cd)))
  11.  
  12. (defun prompt-read (prompt)
  13.   (format *query-io* "~a: " prompt)
  14.   (force-output *query-io*)
  15.   (read-line *query-io*))
  16.  
  17. (defun prompt-for-cd ()
  18.   (make-cd
  19.    (prompt-read "Title")
  20.    (prompt-read "Artist")
  21.    (or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
  22.    (y-or-n-p "Ripped [y/n]: ")))
  23.  
  24. (defun add-cds ()
  25.   (loop (add-record (prompt-for-cd))
  26.       (if (not (y-or-n-p "Another? [y/n]: ")) (return))))
  27.  
  28. (defun save-db (filename)
  29.   (with-open-file (out filename
  30.                        :direction :output
  31.                        :if-exists :supersede)
  32.     (with-standard-io-syntax
  33.       (print *db* out))))
  34.  
  35. (defun load-db (filename)
  36.   (with-open-file (in filename)
  37.     (with-standard-io-syntax
  38.       (setf *db* (read in)))))
  39.  
  40. (defun select (selector-fn)
  41.   (remove-if-not selector-fn *db*))
  42.  
  43. (defun make-comparison-expr (field value)
  44.   `(equal (getf cd ,field) ,value))
  45.  
  46. (defun make-comparisons-list (fields)
  47.   (loop while fields
  48.         collecting (make-comparison-expr (pop fields) (pop fields))))
  49.  
  50. (defmacro where (&rest clauses)
  51.   `#'(lambda (cd) (and ,@(make-comparisons-list clauses))))
  52.  
  53. (defun update (selector-fn &key title artist rating (ripped nil ripped-p))
  54.   (setf *db*
  55.         (mapcar
  56.          #'(lambda (row)
  57.              (when (funcall selector-fn row)
  58.                (if title (setf (getf row :title) title))
  59.                (if artist (setf (getf row :artist) artist))
  60.                (if rating (setf (getf row :rating) rating))
  61.                (if ripped-p (setf (getf row :ripped) ripped)))
  62.              row)
  63.          *db*)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement