2

类似于Clojure recur with multi-arity,我希望 recur 使用不同的 arity。但就我而言,我通过 let 定义函数,因为我想使用 let ( file-list) 中的另一个值而不传递它:

(let [file-list (drive/list-files! google-drive-credentials google-drive-folder-id)
      download-file (fn
                      ([name]
                       (download-file ; <-- attempt to recur
                         name
                         (fn []
                           (throw
                             (ex-info
                               (str name " not found on Google Drive"))))))
                      ([name not-found-fn]
                       (if-let [file (->> file-list
                                          (filter #(= (:original-filename %)
                                                      name))
                                          first)]
                         (drive/download-file! google-drive-credentials file)
                         (not-found-fn))))]
  ;; #
  )

我收到此错误:Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: download-file in this context

4

2 回答 2

4

您可以给 本地名称fn

(let [download-file (fn download-file
                      ([name] (download-file name (fn ...))))

你也可以使用letfn

(letfn [(download-file
          ([name] (download-file name (fn ...)))
          ([name not-found-fn] ...)])
于 2018-12-19T09:17:58.563 回答
-1

为了使异常错误消息更具可读性,我经常附加-fn到内部函数名称,如下所示:

(let [download-file (fn download-file-fn [name] ...) ]
   <use download-file> ...)
于 2018-12-19T15:22:31.700 回答