<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged closure</title>
<link>https://ask.clojure.org/index.php/tag/closure</link>
<description></description>
<item>
<title>ClojureScript and the deprecation of Google Closure Library</title>
<link>https://ask.clojure.org/index.php/13612/clojurescript-and-the-deprecation-google-closure-library</link>
<description>&lt;p&gt;Google &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/google/closure-library/issues/1214&quot;&gt;announced recently&lt;/a&gt; that they will be sunsetting the Closure Library (not Closure itself).&lt;/p&gt;
&lt;p&gt;Does this impact ClojureScript in any meaningful way?&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13612/clojurescript-and-the-deprecation-google-closure-library</guid>
<pubDate>Fri, 12 Jan 2024 16:33:30 +0000</pubDate>
</item>
<item>
<title>How can I make this code more generic/dry?</title>
<link>https://ask.clojure.org/index.php/12650/how-can-i-make-this-code-more-generic-dry</link>
<description>&lt;p&gt;I have several request handlers which all follow a similar pattern. &lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;
(defn handle-data-1
  [req]
  {:pre [(some? req)]}
  (try
    (let [start-date (parse-query-param (-&amp;gt; req :params :start-date))
          end-date (parse-query-param (-&amp;gt; req :params :end-date))
          data (db/fetch-data-1 start-date end-date)
          data-count (count data)]
      (log/debugf &quot;data-count=%d start-date=%s end-date=%s&quot; 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 &quot;%s&quot; error-message)
        {:status 500 :body error-message}))))

(defn handle-data-2
  [req]
  {:pre [(some? req)]}
  (try
    (let [limit (Long/valueOf (parse-query-param (-&amp;gt; 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 &quot;%s&quot; error-message)
        {:status 500 :body error-message}))))

(defn handle-data-3
  [_]
  (try
    (let [data (db/fetch-data-3)
          data-count (count data)]
      (log/debugf &quot;data-count=%d&quot; data-count)
      (if (pos? data-count)
        {:status 200
         :body data}
        {:status 404}))
    (catch Exception e
      (let [error-message (.getMessage e)]
        (log/errorf &quot;%s&quot; error-message)
        {:status 500 :body error-message}))))
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;I'm trying to make a generic function which can be used by all the handle functions.  Something like:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;
(defn try-query [f]
  {:pre [(some? f)]}
  (try
    (let [data (f) ;; &amp;lt;- 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 &quot;%s&quot; error-message)
        {:status 500 :body error-message}))))

(defn handle-data-1 [req]
  (let [start-date (parse-query-param (-&amp;gt; req :params :start-date))
        end-date (parse-query-param (-&amp;gt; req :params :end-date))]
    (log/debugf &quot;start-date=%s end-date=%s&quot; start-date end-date)
    (try-query (fn [] (db/fetch-data-1 start-date end-date))))) ;; &lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Any ideas?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12650/how-can-i-make-this-code-more-generic-dry</guid>
<pubDate>Tue, 07 Feb 2023 19:24:06 +0000</pubDate>
</item>
</channel>
</rss>