如何删除此clojure代码中的冗余?

时间:2014-10-12 21:30:45

标签: clojure

我写了以下代码:

(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))))

在此代码中是否可以轻松删除冗余?

1 个答案:

答案 0 :(得分:4)

记录的字段在记录定义中定义的方法范围内。

(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))))
相关问题