反应式扩展:将 IObservable 中的值配对
给定:
class CharPair
{
char _a;
char _b;
CharPair(char a, char b) { _a = a; _b = b; }
}
IObservable<char> keyPresses = ... // a sequence of key presses
如何获取每两个字符并创建一个新的IObservable
例如
'a','1','b','2','c','3','d','4'
->
CharPair('a','1'),CharPair('b','2'),CharPair('c','3'),CharPair('d','4')
,到目前为止我有以下内容,但这看起来相当冗长,可以改进吗?
IObservable<char> keyPresses = KeyPresses().ToObservable().Publish();
var odds = keyPresses.Where((_,i) => (i&1) == 1);
var evens = keyPresses.Where((_,i) => (i&1) == 0);
IObservable<CharPair> charPairs = evens.Zip(odds, (e, o) => new CharPair(e,o));
Given:
class CharPair
{
char _a;
char _b;
CharPair(char a, char b) { _a = a; _b = b; }
}
IObservable<char> keyPresses = ... // a sequence of key presses
How can I take every two characters and create a new IObservable<CharPair>?
E.g.
'a','1','b','2','c','3','d','4'
->
CharPair('a','1'),CharPair('b','2'),CharPair('c','3'),CharPair('d','4')
So far I have the following but this seems rather long winded, can it be improved on?
IObservable<char> keyPresses = KeyPresses().ToObservable().Publish();
var odds = keyPresses.Where((_,i) => (i&1) == 1);
var evens = keyPresses.Where((_,i) => (i&1) == 0);
IObservable<CharPair> charPairs = evens.Zip(odds, (e, o) => new CharPair(e,o));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BufferWithCount 应该有帮助,你可以这样做:
BufferWithCount should help, you can do something like this: