Coder Social home page Coder Social logo

pcl's Introduction

Practical Common Lisp

chapter 3: practical: a simple database

(defvar *db* nil)
(defun make-cd (title artist rating ripped)
  (list :title title
        :artist artist
        :rating rating
        :ripped ripped))
(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 ()
  "Add a bunch of CDs at once."
  (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))))

This time you don’t need to specify :direction in the options to WITH-OPEN-FILE, since you want the default of :input. And instead of printing, you use the function READ to read from the stream in.

(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*))
(defun select (selector-fn)
  (remove-if-not selector-fn *db*))
(defun artist-selector (artist)
  "Creates an anonymous function."
  #'(lambda (cd) (equal (getf cd :artist) arist)))
(select (artist-selector "Dixie Chicks"))
(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 (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))))
(defmacro where (&rest clauses)
  `#'(lambda (cd) (and ,@(make-comparisons-list clauses))))
<<cddb-paramvars>>

<<cddb-make-cd>>

<<cddb-add-record>>

<<cddb-dump-db>>

<<cddb-prompt-read>>

<<cddb-prompt-for-cd>>

<<cddb-add-cds>>

<<cddb-save-db>>

<<cddb-load-db>>

<<cddb-select>>

<<cddb-artist-selector>>

<<cddb-where-2>>

<<cddb-update>>

<<cddb-delete-rows>>

<<cddb-make-comparison-expr>>

<<cddb-make-comparisons-list>>

chapter 8

(defun primep (number)
  "Checks to see if a number is prime."
  (when (> number 1)
    (loop for fac from 2 to (isqrt number)
          never (zerop (mod number fac)))))
(defun next-prime (number)
  (loop for n from number when (primep n) return n))
(defmacro do-primes ((var start end) &body body)
  `(do ((,var (next-prime ,start) (next-prime (1+ ,var))))
       ((> ,var ,end))
     ,@body))
<<fprimep>>

<<fnextprime>>

<<mdoprimes>>

chapter 9: building a unit testing framework

(defun report-result (result form)
  (format t "~:[FAIL~;pass~] ... ~a~%" result form))
(defmacro check (&body forms)
  `(progn
     ,@(loop for f in forms collect `(report-result ,f ',f))))
<<freportresult>>

<<mcheck>>

pcl's People

Contributors

chumutt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.