如何通过属性将 seq 附加到另一个?
我在 FSharp 中有一个 seq of seq。如果谓词返回 true,我想将一个 seq 加入到前一个 seq 中。
示例:
let items = seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]
我想将一个 seq 加入到前一个 seq 中,如果 seq 以 1 开头,那么结果应该是这样的:
seq [seq[2;3;4;1;5;6;7; 1;9];seq[2;3;5;7]
]
有什么好的功能方法可以做到这一点吗?
我刚刚开始将漫长的计算过程从 C# 转换为 F#,即使经过几个小时的工作以及我对 FSharp 的初级了解,我所实现的性能改进也给我留下了深刻的印象。
我从亚马逊买了一本名为“Beginning F#”的书。它真的很棒,但我现在主要应该使用序列、列表、映射、集合,并且这个主题没有像我需要的那样详细解释。有人愿意为我提供有关这些主题的好资源吗?
提前谢谢!
I have a seq of seqs in FSharp. I want to join a seq to the previous one if a predicate returns to true for it.
Sample:
let items = seq [seq[2;3;4];seq[1;5;6;7;1;9];seq[2;3;5;7]]
I want to join a seq to the previos one, if the seq starts by 1, so the result should be in this case:
seq [seq[2;3;4;1;5;6;7;1;9];seq[2;3;5;7]
]
Is there any nice functional way to do it?
I am just started to translate my long computation processes from C# to F# and very impressed by the performance improvement I could achieve after even a very few hours of work and my beginner level knowledge of FSharp.
I have bought a book from Amazon entitled 'Beginning F#'. It is really great, but I mainly should work with seqs, lists, maps, collections now and this topic isn't explained as detailed as I need. Would anyone be so kind to advise me a good resource about ths topics?
Thx in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与您的最后一个问题一样,没有库函数可以完全执行此操作。最直接的解决方案是使用
IEnumerator
强制编写此代码。但是,您可以编写一个更通用的函数(然后也可以用于其他目的)。为了解决原来的问题,你可以检查序列的第一个元素是否是 1 以外的数字。你将得到一个组序列,其中一个组是序列的序列 - 然后你可以连接这些组:
编辑:我还在此处发布了该函数作为代码片段(具有漂亮的 F# 格式):http://fssnip。净/6A
As with your last question, there is no library function that does exactly this. The most straightforward solution is to write this imperatively using
IEnumerator
. However, you can write a more generally useful function (that can then be used for other purposes too).To solve the original problem, you can then check whether the first element of the sequence is a number other than 1. You'll get a sequence of groups where a group is sequence of sequences - then you can just concatenate the groups:
EDIT: I also posted the function as a snippet (with nice F# formatting) here: http://fssnip.net/6A
正如其他解决方案中所见,这个问题几乎与您的最后一个问题相反。因此,为了更好地衡量,我在这里给出了我的答案的修改版本:
你可以像这样使用它:
As seen in the other solutions, this problem is almost the inverse of your last question. So for good measure, I give a modified version of my answer to that here:
And you use it like so:
对我来说看起来像折叠,如下所示。尝试在没有参考值的情况下尽可能发挥功能。
Looks like a fold to me as shown below. Tried to be as functional as possible without ref values.