Luke
Luke

Reputation: 1806

How do I remove redundancy in this clojure code?

I have written the following code:

(defprotocol MusicFile
    (get-artist [this])
    (get-song [this])
    (get-album [this]))


(defrecord Mp3 [filename]
    MusicFile
    (get-artist [this]
        (let [info (get-all-info (:filename this))]
            (:artist info)))
    (get-song [this]
        (let [info (get-all-info (:filename this))]
            (:title info)))
    (get-album [this]
        (let [info (get-all-info (:filename this))]
            (:album info))))

Is there a easyway to remove redundancy in this code?

Upvotes: 1

Views: 76

Answers (1)

noisesmith
noisesmith

Reputation: 20194

The fields of the record are in scope within methods defined inside the record definition.

(defrecord Mp3 [filename]
    MusicFile
    (get-artist [this]
      (:artist (get-all-info filename)))
    (get-song [this]
      (:title (get-all-info filename)))
    (get-album [this]
      (:album (get-all-info filename))))

Upvotes: 4

Related Questions