Clojure: Idiomatic way to call contains? on a lazy sequence

送分小仙女□ 提交于 2019-11-28 07:15:24

问题


Is there an idiomatic way of determining if a LazySeq contains an element? As of Clojure 1.5 calling contains? throws an IllegalArgumentException:

IllegalArgumentException contains? not supported on type: clojure.lang.LazySeq      
clojure.lang.RT.contains (RT.java:724)

Before 1.5, as far as I know, it always returned false.

I know that calling contains? on a LazySeq may never return as it can be infinite. But what if I know it isn't and don't care if it is evaluated eagerly?

What I came up with is:

(defn lazy-contains? [col key]
  (not (empty? (filter #(= key %) col))))

But it doesn't feel quite right. Is there a better way?


回答1:


First, lazy seqs are not efficient for checking membership. Consider using a set instead of a lazy seq.

If a set is impractical, your solution isn't bad. A couple of possible improvements:

  1. "Not empty" is a bit awkward. Just using seq is enough to get a nil-or-truthy value that your users can use in an if.You can wrap that in boolean if you want true or false.

  2. Since you only care about the first match, you can use some instead of filter and seq.

  3. A convenient way to write an equality predicate is with a literal set, like #{key}, though if key is nil this will always return nil whether nil is found our not.

All together that gives you:

(defn lazy-contains? [col key]
  (some #{key} col))



回答2:


If you use some instead of filter as in your example, you'll get an immediate return as soon as a value is found instead of forcing evaluation of the entire sequence.

(defn lazy-contains? [coll key]
  (boolean (some #(= % key) coll)))

Edit: If you don't coerce the result to a boolean, note that you'll get nil instead of false if the key isn't found.



来源:https://stackoverflow.com/questions/16264813/clojure-idiomatic-way-to-call-contains-on-a-lazy-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!