-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter3.lisp
94 lines (77 loc) · 2.85 KB
/
chapter3.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped ripped))
(defvar *db* nil)
(defun add-record (cd) (push cd *db*))
; do list loops over the list *db* and stores the current iteration
; in cd
; explanation of format function:
; t: print to standard output
; ~{...~} iterate over a list consuming some number of variables from
; the list each time
; ~a print this variable in human readable format
; ~10t previous print so that it takes up 10 columns
; ~% new line
(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-by-artist (artist)
(remove-if-not
#'(lambda (cd) (equal (getf cd :artist) artist))
*db*))
; remove-if-not takes a list and a predicate and returns a new list
; of all elements from the original list that match the predicate
(defun select (selector-fn)
(remove-if-not selector-fn *db*))
(defun artist-selector (artist)
#'(lambda (cd) (equal (getf cd :artist) artist)))
;; (defun where (&key title artist rating (ripped nil ripped-p))
;; #'(lambda (cd)
;; (and
;; (if title (equal (getf cd :title) title) t)
;; (if artist (equal (getf cd :artist) artist) t)
;; (if rating (equal (getf cd :rating) rating) t)
;; (if ripped-p (equal (getf cd :ripped) ripped) t))))
(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*)))
(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))))
; &rest allows function to take arbitrary number of arguments and puts
; them all into the list, "clauses"
(defmacro where (&rest clauses)
`#'(lambda (cd) (and ,@(make-comparisons-list clauses))))