问题描述
在F#中,我们有List.partition
和Array.partition
,它们分别返回列表的元组和数组的元组.
In F# we have List.partition
and Array.partition
which return a tuple of lists and a tuple of arrays respectively.
那么,为什么没有Seq.partition
返回序列的元组?
so, why is there no Seq.partition
returning a tuple of sequences?
这是一个非常简单的实现: F#代码段
here is a very simple implementation:F# Snippets
所以...为什么这不是核心部分?
so... why isn't this part of the core?
推荐答案
在F#4.0(Visual Studio 2015)中,核心库比以前更加统一,但是它们仍然没有实现Seq.partition
.您可以在F#语言设计讨论中找到有关此内容的更多信息:产生两个或多个输出集合的常规函数运算符.
In F# 4.0 (Visual Studio 2015), the core libraries are a lot more uniform than before, but they still do not come with an implementation of Seq.partition
. You can find more about this in the F# language design discussion: Regular functional operators producing two or more output collections.
总结是Seq.partition
函数非常棘手,拥有它可能会引入潜在的性能问题.它有两种工作方式:
The summary is that the Seq.partition
function is quite tricky and a having it could introduce potential performance issues. There a couple of ways it can work:
-
它可以对输入集合进行两次迭代(例如FsSnip版本),如果您有复杂的延迟计算(您要进行两次操作),则可能导致问题
It can iterate over the input collection twice (like the FsSnip version), which can cause issues if you have complex delayed computation (you're doing everything twice)
它可以遍历一次输入,但是随后必须进行一些复杂的可变状态共享(可以秘密分配内存).
It can iterate over the input once, but then it would have to do some complex mutable state sharing (which could secretly allocate memory).
因此,在保留seq<'T>
类型的所有良好属性的同时,不能合理地实现Seq.partition
.
So, Seq.partition
cannot be implemented reasonably while keeping all the good properties that you would expect about the seq<'T>
type.
这篇关于为什么F#中没有Seq.partition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!