Doto and property setting conditionally

I want to write:

(defn download-web-page
    "Downloads the webpage at the given url and returns its contents."
    [^String url ^String user ^String password]
    (with-open [client (doto (WebClient.)
                       (when user (.set_Credentials (NetworkCredential. user password ""))))]
    (.DownloadString client url)))

Therefore, I want to set credentials only if they are given as an argument to the function. However, this does not seem to be the case, and it is not, when I replace the if with the if.

The function works fine if I delete it at all.

I think I can not use when in doto - are there any good ways to write this?

+3
source share
4 answers

I would just write it as:

(defn download-web-page
  "Downloads the webpage at the given url and returns its contents."
  [^String url ^String user ^String password]
  (with-open [client (WebClient.)]
    (when user
      (.set_Credentials client (NetworkCredential. user password "")))
    (.DownloadString client url)))

with-opendoes not impose any special requirements on client, except that it has a no-arguments method close, so you do not need to “end” it in any sense in the binding vector.

+3
source

(: , , , . , .)

(defn download-web-page
  "Downloads the webpage at the given url and returns its contents."
  ([^String url] (download-web-page url nil nil))
  ([^String url ^String user ^String password]
     (with-open [client (doto (WebClient.)
                          (-> (.set_Credentials
                               (NetworkCredential. user password ""))
                              (->> (when user))))]
       (.DownloadString client url))))

. :

(defn download-web-page
  "Downloads the webpage at the given url and returns its contents."
  ([^String url] (download-web-page url nil nil))
  ([^String url ^String user ^String password]
    (with-open [client (let [c (WebClient.)]
                         (when user
                           (.set_Credentials
                            (NetworkCredential. user password "")))
                         c)]
      (.DownloadString client url))))

->/->> :

(defmacro doto-guard [guard action]
  `(-> ~action ~guard))

(doto (WebClient.)
  (doto-guard (when user) (.setCredentials ...)))

, doto doto. , , . let .

( , ... , , , ~guard (when ~guard), (doto-guard user (.setCredentials ...)). ).

- - nil nil, .

+4
(defn download-web-page
  "Downloads the webpage at the given url and returns its contents."
  [^String url ^String user ^String password]
  (let [client (WebClient.)]
    (when user 
      (.set_Credentials client (NetworkCredential. user password "")))
    (with-open [client client]
      (.DownloadString client url)))

(with-open [client client]... , , . doto , , :

(defn build-web-client
  [^String user ^String password]
  (let [client (WebClient.)]
    (when user 
      (.set_Credentials client (NetworkCredential. user password "")))
    client))

(defn download-web-page
  "Downloads the webpage at the given url and returns its contents."
  [^String url ^String user ^String password]
    (with-open [client (build-web-client user password)]
      (.DownloadString client url)))
+2

cond-> doto :

(doto (java.util.Vector.)
  (cond-> (= 1 1) (.add "bla")))
;; => ["bla"]

(doto (java.util.Vector.)
  (cond-> (= 1 2) (.add "bla")))
;; => []

.

0

Source: https://habr.com/ru/post/1764781/


All Articles