English 中文(简体)
有条件地处置和确定财产
原标题:doto and setting property conditionally
  • 时间:2010-09-14 22:12:41
  •  标签:
  • clojure

我想写:

(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)))

因此,我只想提出全权证书,作为职务的理由。 然而,这似乎并不奏效,在我用某种方式取代时段时,也没有这样做。

如果我完全取消这一职能,那么这项职能就会被罚款。

我猜想,在什么时候,我会利用某种方式来写这句话?

最佳回答

我只想写一下:

(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)))

因此,你不需要在具有约束力的病媒内从任何意义上“确定”。

问题回答

(说明:这都应充满希望地发挥作用,但我此时无法测试。) 请对你本人的卫生检查。

页: 1

(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))))

The convoluted ->->> patterns from the first edition can be抽象地删除:

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

然后,请你写

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

在固定<代码>doto条款中加以混合时,有你可以多次使用的冰层财产。 如果在你的法典中更经常地提到这种事情,那是ice的。 否则,<代码>let-based edition should dovy.

(如果这种模式出现really)。 通常,对你们来说,宏观可以变得更加灵活...... 它还试图使其稍微达到<>无<>><>>>>><>><>>> 灵活,但先决条件是,将<条码>改为<条码>(时有条码),以便在使用时填写<条码>。 然而,选择特定版本的任何深层理由都必须从更广泛的角度看待。

分成两个职能机构只是一个风格问题——我更倾向于在没有实际提供全权证书的情况下不写<代码>nil nil。

页: 1 同样:

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

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

赋予你有条件的教条。

(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)))

<代码>(与开放式[客户][客户]......>看着一种比喻,但却是过错,它带有一种新式的州法。 如果<代码>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)))




相关问题
How to improve Clojures error messages

I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...

clojure rmi classpath problem

I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...

Help translating this Java codeblock to Clojure?

I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

how to efficiently apply a medium-weight function in parallel

I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...

热门标签