我正在创建与elasticsearch的连接(但在这里替换您喜欢的任何其他数据源),它将在运行时基于环境或配置文件参数。看起来像这样:
(defn create-conn
"Connect to the given uri. This is a persistent conn managed by clj-http (apache)."
([uri]
( ;;; create a persistent connection using clj-http / elastic
...)
([]
(create-conn (or (System/getenv "ES_URL")
(cfg/get-url-from-config-file)
"http://localhost:9200"))))
Run Code Online (Sandbox Code Playgroud)
因为连接在服务器的生存期内不会改变,所以我只需要运行一次此函数并缓存结果。有几种方法可以做到这一点:
1- memoize它-虽然可行,但由于我只想缓存一件事,因此感觉不像是正确的方法
2-使用状态管理器,例如Component或mount;由于我不是真正地管理状态,只是设置和忘记,并且仅将其用于这一件事,所以感觉有点过分了。例如,以下内容如何优于下面的#3?
; mount version -- good, but how is this better than #3 below?
(defstate conn :start (getconn))
(mount/start #'elastic/conn) ;; somewhere else, must start mount
Run Code Online (Sandbox Code Playgroud)
3- def它。尽管运行create-conn实际上并没有执行任何网络活动,但我宁愿不在文件加载时运行它,如果我只是对其进行常规操作def,就会发生这种情况,因此我必须执行以下操作。请注意,该getconn功能是为了方便起见,因此我不必deref conn直接:
(def conn …Run Code Online (Sandbox Code Playgroud) 我是Clojure的新人.我在Clojure Koans的帮助下学习.我找到了以下代码的答案:
(= ["Real Jerry" "Bizarro Jerry"]
(do
(dosync
(ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world assoc :jerry "Bizarro Jerry")
(vec (map #(:jerry @%) [the-world bizarro-world]))))))
Run Code Online (Sandbox Code Playgroud)
来自:https://github.com/viebel/clojure-koans/blob/master/src/koans/16_refs.clj#L42
谷歌搜索"Clojure @%"是非常不友好的.所以我从互联网上得不到什么.
它如何用于"#(:jerry @%)"功能?
以下代码是我的答案,但它不起作用.
(= ["Real Jerry" "Bizarro Jerry"]
(do
(dosync
(ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world assoc :jerry "Bizarro Jerry")
(vec (map (fn [x] (:jerry x)) [the-world bizarro-world]))
)))
Run Code Online (Sandbox Code Playgroud) 在spring项目中,我想LocalDate从@Autowired构造函数参数创建一个值,该参数的值在.properties文件中。我想做两件事:
my.date,则应通过解析属性值来创建参数设置属性后,以及使用以下命令时:
@DateTimeFormat(pattern = "yyyy-MM-dd") @Value("${my.date}") LocalDate myDate,
...
Run Code Online (Sandbox Code Playgroud)
我收到此错误:java.lang.IllegalStateException:无法将类型“ java.lang.String”的值转换为所需类型“ java.time.LocalDate”:找不到匹配的编辑器或转换策略
我还使用iso = ...来使用具有相同结果的ISO日期。
LocalDate.now()我尝试使用默认值,例如:
@Value("${my.date:#{T(java.time.LocalDate).now()}}") LocalDate myDate,
...
Run Code Online (Sandbox Code Playgroud)
但是我得到了同样的错误。
原谅我对Spring的无知,但是如何在这里实现两个目标?
我有一只DataFrame看起来如下的熊猫:
sample = pd.DataFrame([[2,3],[4,5],[6,7],[8,9]],
index=pd.date_range('2017-08-01','2017-08-04'),
columns=['A','B'])
A B
2017-08-01 2 3
2017-08-02 4 5
2017-08-03 6 7
2017-08-04 8 9
Run Code Online (Sandbox Code Playgroud)
我想将这些值累加到列中.以列A为例,第二行变为2*4第三行2*4*6,最后一行变为2*4*6*8.B列也是如此.所以,期望的结果是:
A B
2017-08-01 2 3
2017-08-02 8 15
2017-08-03 48 105
2017-08-04 384 945
Run Code Online (Sandbox Code Playgroud)
必须有一些内置的方法来做到这一点,但由于链式分配问题,我甚至在使用for循环时遇到了问题.