Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

104 C HAPTER 4 Polymorphism with multimethods Listing 4.1 Implementing the Redis protocol using multimethods (defmulti parse-reply reply-type :default :unknown) (defmethod parse-reply :unknown [#^BufferedReader reader] (throw (Exception. (str "Unknown reply type:")))) (defmethod parse-reply \- [#^BufferedReader reader] (let [error (read-line-crlf reader)] (throw (Exception. (str "Server error: " error))))) (defmethod parse-reply \+ [#^BufferedReader reader] (read-line-crlf reader)) (defmethod parse-reply \$ [#^BufferedReader reader] (let [line (read-line-crlf reader) length (parse-int line)] (if (< length 0) nil (let [#^chars cbuf (char-array length)] (do (do-read reader cbuf 0 length) (read-crlf reader) ;; CRLF (String. cbuf))))))