如何找到惰性序列的长度而不强制实现? [英] How to find length of lazy sequence without forcing realization?

查看:111
本文介绍了如何找到惰性序列的长度而不强制实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读O'reilly Clojure编程书,其中有关于延迟序列的以下部分:

I'm currently reading the O'reilly Clojure programming book which it's says the following in it's section about lazy sequences:


是可能的(虽然非常罕见),为了延迟序列知道它的长度,因此返回它作为计数的结果,而不实现其内容。

It is possible (though very rare) for a lazy sequence to know its length, and therefore return it as the result of count without realizing its contents.

我的问题是,这是怎么做的,为什么这么少?

My question is, How this is done and why it's so rare?

不幸的是,这本书没有指定这些内容。我个人认为,在它的实现之前知道延迟序列的长度是非常有用的,例如,在同一页中是一个使用 map <>的函数处理的延迟文件序列的例子。 / code>。这将是很高兴知道在实现序列之前可以处理多少文件。

Unfortunately, the book does not specify these things in this section. I personally think that it's very useful to know the length of a lazy sequence prior it's realization, for instance, in the same page is an example of a lazy sequence of files that are processed with a function using map. It would be nice to know how many files could be processed before realizing the sequence.

推荐答案

我想这是因为通常还有其他方法来找出大小。

I suppose it's due to the fact that usually there are other ways to find out the size.

现在可以想到的唯一序列实现是可能做到的,是某种昂贵的函数的映射/程序在已知大小的集合。

The only sequence implementation I can think of now that could potentially do that, is some kind of map of an expensive function/procedure over a known size collection.

一个简单的实现将返回底层集合的大小,同时推迟延迟序列的元素的实现(并因此执行昂贵的部分)直到必要。

A simple implementation would return the size of the underlying collection, while postponing realization of the elements of the lazy-sequence (and therefore execution of the expensive part) until necessary.

在这种情况下,可以预先了解要映射的集合的大小,并可以使用它而不是延迟seq大小。

In that case one knows the size of the collection that is being mapped over beforehand and can use that instead of the lazy-seq size.

这可能是有用的,这就是为什么它不是不可能实现,但我猜是很少需要。

It might be handy sometimes and that's why it's not impossible to implement, but I guess rarely necessary.

这篇关于如何找到惰性序列的长度而不强制实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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