clojure - Clojure 中的 seq 函数注意事项

标签 clojure

在 clojure 的 seq 函数的文档字符串中,它提到:

Note that seqs cache values, thus seq should not be used on any Iterable whose iterator repeatedly returns the same mutable object.

这句话是什么意思?为什么要强调同一个可变对象?

最佳答案

评论是added later并提到这个ticket :

Some Java libraries return iterators that return the same mutable object on every call:

  • Hadoop ReduceContextImpl$ValueIterator
  • Mahout DenseVector$AllIterator/NonDefaultIterator
  • LensKit FastIterators

While careful usage of seq or iterator-seq over these iterators worked in the past, that is no longer true as of the changes in CLJ-1669 - iterator-seq now produces a chunked sequence. Because next() is called 32 times on the iterator before the first value can be retrieved from the seq, and the same mutable object is returned every time, code on iterators like this now receives different (incorrect) results.

Approach: Sequences cache values and are thus incompatible with holding mutable and mutating Java objects. We will add some clarification about this to seq and iterator-seq docstrings. For those iterators above, it is recommended to either process those iterators in a loop/recur or to wrap them in a lazy-seq that transforms each re-returned mutable object into a proper value prior to caching.

关于clojure - Clojure 中的 seq 函数注意事项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493735/

相关文章:

clojure - 在抛出异常之前重试某件事 3 次 - 在 clojure 中

data-structures - 如何实现递归deftype

data-structures - 在 Clojure 中表示一棵树

clojure - 如何使用 "lein uberjar"正确创建 uberjar?

lisp - 我应该学习哪种 Lisp?

clojure - 如何在 Clojure 中编写/执行类型操作?

algorithm - 如何在 Clojure 算法实现中处理多个变量?

clojure - 如何在 ClojureScript 中使用 Reanimated "worklet"指令?

clojure - Clojure函数(第n个[coll索引])和组合(last(取索引coll))有什么区别

tomcat - 如何在 lein-ring uberwar 中添加 META-INF/context.xml