clojure - how to separate concerns in the below fn -


the function below 2 things -

  • checks if atom nil or fetch-agin true, , fetches data.
  • it processes data calling (add-date-strings).

what better pattern separate out above 2 concerns ?

(def retrieved-data (atom nil))  (defn fetch-it!   [fetch-again?]   (if (or fetch-again?             (nil? @retrieved-data))     (->> (exec-services)          (map #(add-date-strings (:time %)))          (reset! retrieved-data))     @retrieved-data)) 

one possible refactoring be:

(def retrieved-data (atom nil))  (defn fetch []   (->> (exec-services)        (map #(add-date-strings (:time %)))))  (defn fetch-it!   ([]      (fetch-it! false))   ([force]      (if (or force (nil? @retrieved-data))        (reset! retrieved-data (fetch))        @retrieved-data))) 

by way, pattern seperate out concerns called "functions" :)


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -