Guest User

Untitled

a guest
Sep 27th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 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. ;; returns Plist that represents a CD
  18. (defun prompt-for-cd ()
  19. (make-cd
  20. (prompt-read "Title")
  21. (prompt-read "Artist")
  22. (or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
  23. (y-or-n-p "Ripped (y/n)")))
  24.  
  25. ;; add a bunch of CDs
  26. (defun add-cds ()
  27. (loop (add-record (prompt-for-cd))
  28. (if (not (y-or-n-p "Another? (y/n): ")) (return))))
  29.  
  30.  
  31. (defun save-db (filename)
  32. (with-open-file (out filename
  33. :direction :output
  34. :if-exists :supersede)
  35. (with-standard-io-syntax
  36. (print *db* out))))
  37.  
  38. (defun load-db (filename)
  39. (with-open-file (in filename)
  40. (with-standard-io-syntax
  41. (setf *db* (read in)))))
  42.  
  43. ;;like sql select statement
  44. (defun select (selector-fn)
  45. (remove-if-not selector-fn *db*))
  46.  
  47. (defun artist-selector (artist)
  48. #'(lambda (cd) (equal (getf cd :artist) artist)))
  49.  
  50. (defun where (&key title artist rating (ripped nil ripped-p))
  51. #'(lambda (cd)
  52. (and
  53. (if title (equal (getf cd :title title) t))
  54. (if artist (equal (getf cd :artist) artist) t)
  55. (if rating (equal (getf cd :rating) rating) t)
  56. (if ripped-p (equal (getf cd :ripped) ripped) t))))
  57.  
  58. (defun update (selector-fn &key title artist rating (ripped nil ripped-p))
  59. (setf *db*
  60. (mapcar
  61. #'(lambda (row)
  62. (when (funcall selector-fn row)
  63. (if title (setf (getf row :title) title))
  64. (if artist (setf (getf row :artist) artist))
  65. (if rating (setf (getf row :rating) rating))
  66. (if ripped-p (setf (getf row :ripped) ripped)))
  67. row) *db* )))
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment