I have several request handlers which all follow a similar pattern.
(defn handle-data-1
[req]
{:pre [(some? req)]}
(try
(let [start-date (parse-query-param (-> req :params :start-date))
end-date (parse-query-param (-> req :params :end-date))
data (db/fetch-data-1 start-date end-date)
data-count (count data)]
(log/debugf "data-count=%d start-date=%s end-date=%s" data-count start-date end-date)
(if (pos? data-count)
{:status 200
:body data}
{:status 404}))
(catch Exception e
(let [error-message (.getMessage e)]
(log/errorf "%s" error-message)
{:status 500 :body error-message}))))
(defn handle-data-2
[req]
{:pre [(some? req)]}
(try
(let [limit (Long/valueOf (parse-query-param (-> req :params :limit)))
data (db/fetch-data-2 limit)
data-count (count data)]
(if (pos? data-count)
{:status 200
:body data}
{:status 404}))
(catch Exception e
(let [error-message (.getMessage e)]
(log/errorf "%s" error-message)
{:status 500 :body error-message}))))
(defn handle-data-3
[_]
(try
(let [data (db/fetch-data-3)
data-count (count data)]
(log/debugf "data-count=%d" data-count)
(if (pos? data-count)
{:status 200
:body data}
{:status 404}))
(catch Exception e
(let [error-message (.getMessage e)]
(log/errorf "%s" error-message)
{:status 500 :body error-message}))))
I'm trying to make a generic function which can be used by all the handle functions. Something like:
(defn try-query [f]
{:pre [(some? f)]}
(try
(let [data (f) ;; <- need to invoke function and store results in data
data-count (count data)]
(if (pos? data-count)
{:status 200
:body data}
{:status 404}))
(catch Exception e
(let [error-message (.getMessage e)]
(log/errorf "%s" error-message)
{:status 500 :body error-message}))))
(defn handle-data-1 [req]
(let [start-date (parse-query-param (-> req :params :start-date))
end-date (parse-query-param (-> req :params :end-date))]
(log/debugf "start-date=%s end-date=%s" start-date end-date)
(try-query (fn [] (db/fetch-data-1 start-date end-date))))) ;;
The generic function needs to take another function (or closure) as a parameter and it can be of varying arity. I want that generic function to invoke the passed-in function and store its results in a variable.
In Ruby I could do this by passing a lambda to the function, then inside the function call the lambda. I'm not sure how to achieve this in Clojure.
Any ideas?