From ebe856b45df8704c0e5119fb9d22409e936459eb Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Thu, 10 Feb 2011 19:13:52 -0600 Subject: [PATCH] Add datatypes. --- src/koans/datatypes.clj | 45 +++++++++++++++++++++++++++++++++++ src/path_to_answer_sheet.clj | 11 +++++++++ src/path_to_enlightenment.clj | 3 ++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/koans/datatypes.clj diff --git a/src/koans/datatypes.clj b/src/koans/datatypes.clj new file mode 100644 index 0000000..5e301ba --- /dev/null +++ b/src/koans/datatypes.clj @@ -0,0 +1,45 @@ +(defrecord Nobel [prize]) +(deftype Pulitzer [prize]) + +(defprotocol Award + (present [this recipient])) + +(defrecord Oscar [category] + Award + (present [this recipient] + (print (str "Congratulations on your " + (:category this) " Oscar, " + recipient + "!")))) + +(deftype Razzie [category] + Award + (present [this recipient] + __)) + +(meditations + "Holding records is meaningful only when the record is worthy of you" + (= __ (.prize (Nobel. "peace"))) + + "Types are quite similar" + (= __ (.prize (Pulitzer. "literature"))) + + "Records may be treated like maps" + (= __ (:prize (Nobel. "physics"))) + + "While types may not" + (= __ (:prize (Pulitzer. "poetry"))) + + "Further study reveals why" + (= __ + (map map? [(Nobel. "chemistry") + (Pulitzer. "music")])) + + "Either sort of datatype can define methods in a protocol" + (= __ + (with-out-str (present (Oscar. "Best Picture") "Evil Alien Conquerors"))) + + "Surely we can implement our own by now" + (= "You're really the Worst Picture, Final Destination 5... sorry." + (with-out-str (present (Razzie. "Worst Picture") "Final Destination 5")))) + diff --git a/src/path_to_answer_sheet.clj b/src/path_to_answer_sheet.clj index 19977d3..6487c43 100644 --- a/src/path_to_answer_sheet.clj +++ b/src/path_to_answer_sheet.clj @@ -143,6 +143,17 @@ ''(+ 9 1) ''(* 10 2) ''(+ 10 (2 * 3))]} + "datatypes" {"__" ['(print + (str "You're really the " + (.category this) + ", " recipient "... sorry.")) + "peace" + "literature" + "physics" + nil + [true false] + "Congratulations on your Best Picture Oscar, Evil Alien Conquerors!" + ]} }) (defn replace-with [s k replacements] diff --git a/src/path_to_enlightenment.clj b/src/path_to_enlightenment.clj index bc29c25..ecc1216 100644 --- a/src/path_to_enlightenment.clj +++ b/src/path_to_enlightenment.clj @@ -34,7 +34,8 @@ "recursion" "destructuring" "refs" - "macros"]) + "macros" + "datatypes"]) (defn require-version [[required-major required-minor]] (let [{:keys [major minor]} *clojure-version*]