clojure - 当谓词为 false 时需要花一段时间来包含最后一项

dag*_*da1 6 clojure

我有以下使用的函数take-while

(defn process [[cash amount wrappers]]
  (let [bought (int (Math/floor (/ cash amount)))
        free (->>
              (iterate (partial unwrapper wrappers) bought)
              (take-while (partial (fn [w a]
                                     (prn (str "a = " a))
                                     (>= a w)
                                     ) wrappers)))]
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当谓词为 false 时,我想包含最后一项,但take-while不返回最后一项。

有没有办法解决这个问题,take-while或者我应该使用其他东西?

Die*_*sch 5

您可以根据以下来源执行类似的操作take-while

(defn take-while+
  [pred coll]
  (lazy-seq
    (when-let [[f & r] (seq coll)]
      (if (pred f)
        (cons f (take-while+ pred r))
        [f]))))
Run Code Online (Sandbox Code Playgroud)