逆 IEnumerable.SelectMany?
是否有 IEnumerable.SelectMany
的逆/补?也就是说,是否存在 IEnumerable
形式的方法,它将在输入序列中查找序列并执行转换为单个序列元素然后再次将整个事物展平?
例如,如果您想用单个字节 0x7E
替换 HDLC 帧中的所有转义序列 { 0x7E, 0x7E }
,您可以执行类似的
byte[] byteArray = new byte[] { 0x01, 0x02, 0x7E, 0x7E, 0x04 }; // etc.
byte[] escapeSequence = new byte[] { 0x7E, 0x7E };
byte[] outputBytes = byteArray.InverseSelectMany<byte,byte>(x =>
{
if (x.SequenceEqual(escapeSequence))
{
return new List<byte> { 0x7E };
{
else
{
return x;
}
});
操作感觉还是我在这里错过了一些关键的东西?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有这样的内置东西。第一个问题是,通过将任意
Func,T>
传递给枚举器,它不会知道需要“获取”多少字节并传递给函数。下面显示了一种更合理的方法,您可以传递一个要替换的序列,以及另一个要替换的序列,并对其进行简单的搜索。There isn't anything built-in like that. The first problem is that by passing an arbitrary
Func<IEnumerable<T>,T>
to the enumerator, it won't know how many bytes it will need to "take" and pass to the function. A more reasonable approach is shown below, where you can pass a sequence to be replaced, and the other sequence to replace, and do a simple search for that.我不知道这是否对其他人有帮助,但这在我看来是“相反的”。例如,如果您有一个带有 {1, 2, 3} 的对象 a.Items 和带有 { 的 b.Items 3, 4, 5} 调用 DeselectMany 将为您提供 6 个 KeyValuePairs 1:a, 2:a, 3:a, 3:b, 4:b, 5:b。您可以选择调用重载,让您返回除 KeyValuePair 之外的其他内容
I don't know if this would help anybody else, but this is "inverse" in my mind. If, for example, you had an object a.Items with {1, 2, 3} and b.Items with {3, 4, 5} calling DeselectMany will give you 6 KeyValuePairs of 1:a, 2:a, 3:a, 3:b, 4:b, 5:b. Optionally you can call the overload that lets you return something other than KeyValuePair