Clojure Koans 19 Datatypes Notebook

Spoilers to the Clojure Koans exercises below!

Original Clojure Koans repository: https://github.com/functional-koans/clojure-koans/

{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
        ;; complient is used for autocompletion
        ;; add your libs here (and restart the runtime to pick up changes)
        compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.1s
Clojure

Initial Thoughts

...

(defrecord Nobel [prize])
;; question 19001: What is the function/role of `prize` here? It doesn't appear to be defined as per syntax I have seen before...
;; question 19002: Is prize an attribute/property or something else?
Clojure
(deftype Pulitzer [prize]) ;; question: What is `deftype`?
;; question 19003: What exactly is `defrecord`, and what is its role and function?
;; question 19004: Why use a record or a type in Clojure?
Clojure
(defprotocol Award
    (present [this recipient]))
;; description: An protocol named "Award" is created/defined (?) here with
;;              a present "method"/function (?)
;;              which takes (two parameters?)
;;                   a this instance (?)
;;                   and a recipient string (?).
;; question 19005: What is `defprotocol`?
;; answer 19005a: A protocol is similar to an interface. (?)
;; question 19006: What can you do with protocols?
;; answer 19006a: With protocols, you can define interfaces for records and for types.
Clojure
(defrecord Oscar [category]
    Award
    (present [this recipient]
             (print (str "Congratulations on your "
                         (:category this) " Oscar, "
                         recipient
                         "!"))))
;; description: The Oscar record implements the Award protocol. The `this` refers to the instance of the Oscar.
;; skill 19001: Create a record which implements a protocol.
;; question 19007: What are typical/common/useful example protocols?
Clojure
(deftype Razzie [category]
    Award
    (present [this recipient]
             (print (str "You're really the " (.category this) ", " recipient "... sorry."))))
;; skill 19002: Create a type (?) which implements a protocol
;; question 19008: How are types typically (hah) referred to in Clojure? "deftypes", types, or something else?
Clojure
;; "Holding records is meaningful only when the record is worthy of you"
(= "peace" (.prize (Nobel. "peace")))
;; description: Create an instance of a Nobel record with one argument of "peace", then access its "prize" attribute
;; question 19009: What is a record in Clojure?
;; question 19010: What is the dot "." prefix meaning in Clojure?
;; question 19011: What is the dot "." suffix meaning in Clojure?
;; answer 19011a: The dot suffix indicates a constructor function which creates an instance here of records.
;; skill 19003: Construct an "instance" of a type by using `defrecord` (?)
Clojure
;; "Types are quite similar"
(= "literature" (.prize (Pulitzer. "literature")))
;; question 19012: How are types and records different in Clojure? Why have both?
;; question 19013: What is `.prize` here functioning as? Is "accessor" or "getter" the preferred way to label it, or is there a better/more accepted/correct term? 
Clojure
;; "Records may be treated like maps"
(= "physics" (:prize (Nobel. "physics")))
Clojure
;; "While types may not"
(= nil (:prize (Pulitzer. "poetry")))
;; question 19018: What is the utility of this info, "Records may be treated like maps by using keywords to access properties/attributes, but types do not allow for this map-like access." ?
;; question 19014: What is the implementation behind-the-scenes of types in Clojure?
Clojure
;; "Further study reveals why"
(= [true false]
   (map map? [(Nobel. "chemistry")
              (Pulitzer. "music")]))
;; question 19015: If records are maps (in implementation, functionality, or both?), then what are the differences between records and maps?
Clojure
;; "Either sort of datatype can define methods in a protocol"
(= "Congratulations on your Best Picture Oscar, Evil Alien Conquerors!"
   (with-out-str (present (Oscar. "Best Picture") "Evil Alien Conquerors")))
;; question 19016: What is a protocol in Clojure?
Clojure
;; "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")))
;; question 19017: Are deftype and defrecord used often by Clojure developers?
Clojure

Closing Thoughts

...

Runtimes (1)