【问题标题】:clj-record: when is :before-save called?clj-record: :before-save 什么时候调用?
【发布时间】:2011-07-28 16:05:22
【问题描述】:

我在 clj-record 中使用rand 将字段值分配为:before-save 函数。下面的random-str函数用于生成值。

(def VALID-CHARS
    (map char (concat (range 48 58) ; 0-9
        (range 65 91) ; A-Z
        (range 97 123)))) ; a-z

(defn random-char []
    (nth VALID-CHARS (rand (count VALID-CHARS))))

(defn random-str [length]
    (apply str (take length (repeatedly random-char))))

当我随后使用 clj-record create 函数返回的映射时,该字段的值与数据库中的值不同。这是否意味着有一些懒惰的评估怪异正在发生? random-str 是否被多次调用?

【问题讨论】:

  • psst。 (defn random-char [] (rand-nth VALID-CHARS))

标签: random clojure lazy-evaluation


【解决方案1】:

这听起来像是 clj-record 的问题。

只有使用惰性序列或 delays 才能在 clojure 中获得惰性。 否则,clojure 是一种严格/热切评估的语言。

【讨论】:

  • 这是我的怀疑。我不明白为什么 rand 会被多次评估,除非在 clj-record 中使用了原子或引用。事务中涉及的函数应该没有副作用,rand 有状态,所以我想我会放下来体验一下。
猜你喜欢
  • 2019-12-25
  • 2014-03-27
  • 2010-10-18
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多