`count'在Clojure中实现一个延迟序列吗? [英] Does `count` realize a lazy sequence in Clojure?

查看:94
本文介绍了`count'在Clojure中实现一个延迟序列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个LazySeq


(def s(take 10(iterate + 0)))


(count s)实现序列吗?

  user> 

(def s(map#(do(printlndoing work)%)(range 4)))
#'user / s
user> (计数)
做工作
做工作
做工作
做工作
4

一些数据结构可以在常量时间给出答案,尽管惰性序列没有存储计数,并且始终实现它们


Let's say I have a LazySeq

(def s (take 10 (iterate + 0)))

Does (count s) realize the sequence?

解决方案

If you are asking about lazy sequences, Yes.

user> (def s (map #(do (println "doing work") %) (range 4)))
#'user/s
user> (count s)
doing work
doing work
doing work                       
doing work
4  

Some of the data structures can give you answers in constant time, though lazy sequences do not have a stored count, and counting always realizes them.

这篇关于`count'在Clojure中实现一个延迟序列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆