Miki Tebeka committed 50392a2

chapter 3

Comments (0)

Files changed (1)

+(defun make-cd (title artist rating ripped)
+  (list :title title :artist artist :rating rating :ripped ripped))
+(defvar *db* nil)
+(setf *db* nil) ; Reset in case of reload
+(defun add-record (cd)
+  (push cd *db*))
+(defun dump-db ()
+  (dolist (cd *db*)
+    (format t "~{~a:~10t~a~%~}~%" cd)))
+(defun prompt-read (prompt)
+  (format *query-io* "~a: " prompt)
+  (force-output *query-io*)
+  (read-line *query-io*))
+(defun prompt-for-cd ()
+  (make-cd
+   (prompt-read "Title")
+   (prompt-read "Artist")
+   (or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
+   (y-or-n-p "Ripped [y/n]: ")))
+(defun add-cds ()
+  (loop (add-record (prompt-for-cd))
+      (if (not (y-or-n-p "Another? [y/n]: ")) (return))))
+(defun save-db (filename)
+  (with-open-file (out filename
+                   :direction :output
+                   :if-exists :supersede)
+    (with-standard-io-syntax
+      (print *db* out))))
+(defun load-db (filename)
+  (with-open-file (in filename)
+    (with-standard-io-syntax
+      (setf *db* (read in)))))
+(defun select (selector-fn)
+  (remove-if-not selector-fn *db*))
+(defun make-comparison-expr (field value)
+  `(equal (getf cd ,field) ,value))
+(defun make-comparisons-list (fields)
+  (loop while fields
+        collecting (make-comparison-expr (pop fields) (pop fields))))
+(defmacro where (&rest clauses)
+  `#'(lambda (cd) (and ,@(make-comparisons-list clauses))))
+(defun update (selector-fn &key title artist rating (ripped nil ripped-p))
+  (setf *db*
+        (mapcar
+          #'(lambda (row)
+              (when (funcall selector-fn row)
+                (if title    (setf (getf row :title) title))
+                (if artist   (setf (getf row :artist) artist))
+                (if rating   (setf (getf row :rating) rating))
+                (if ripped-p (setf (getf row :ripped) ripped)))
+              row) *db*)))
+(defun delete-rows (selector-fn)
+  (setf *db* (remove-if selector-fn *db*)))
+; Push some records
+(add-record (make-cd "Roses" "Kathy Mattea" 7 t))
+(add-record (make-cd "Fly" "Dixie Chicks" 8 t))
+(add-record (make-cd "Home" "Dixie Chicks" 9 t))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.