Guest User

Untitled

a guest
Aug 14th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. Why do I NOT need Publish on this cold observable?
  2. var subject = new List<string>
  3. {
  4. "test",
  5. "test",
  6. "hallo",
  7. "test",
  8. "hallo"
  9. }.ToObservable();
  10. subject
  11. .GroupBy(x => x)
  12. .SelectMany(grouped => grouped.Scan(0, (count, _) => ++count)
  13. .Zip(grouped, (count, chars) => new { Chars = chars, Count = count }))
  14. .Subscribe(result => Console.WriteLine("You typed {0} {1} times",
  15. result.Chars, result.Count));
  16.  
  17. // I Would have expect that I need to use Publish like that
  18. //subject
  19. // .GroupBy(x => x)
  20. // .SelectMany(grouped => grouped.Publish(sharedGroup =>
  21. // sharedGroup.Scan(0, (count, _) => ++count)
  22. // .Zip(sharedGroup, (count, chars) =>
  23. // new { Chars = chars, Count = count })))
  24. // .Subscribe(result => Console.WriteLine("You typed {0} {1} times",
  25. // result.Chars, result.Count));
  26.  
  27. Console.ReadLine();
  28.  
  29. var subject = new List<Func<string>>
  30. {
  31. () =>
  32. {
  33. Console.WriteLine("performing");
  34. return "test";
  35. },
  36. () => "test",
  37. () => "hallo",
  38. () => "test",
  39. () => "hallo"
  40. }.ToObservable();
  41.  
  42.  
  43. subject
  44. .Select(x => x())
  45. .GroupBy(x => x)
  46. .SelectMany(grouped => grouped.Scan(0, (count, _) => ++count)
  47. .Zip(grouped, (count, chars) => new { Chars = chars, Count = count }))
  48. .Subscribe(result => Console.WriteLine("You typed {0} {1} times",
  49. result.Chars, result.Count));
  50.  
  51. class Program
  52. {
  53. static void Main(string[] args)
  54. {
  55. var subject = new MyObservable();
  56.  
  57. subject
  58. .GroupBy(x => x)
  59. .SelectMany(grouped => grouped.Scan(0, (count, _) => ++count)
  60. .Zip(grouped, (count, chars) => new { Chars = chars, Count = count }))
  61. .Subscribe(result => Console.WriteLine("You typed {0} {1} times",
  62. result.Chars, result.Count));
  63.  
  64. Console.ReadLine();
  65. }
  66. }
  67.  
  68. class MyObservable : IObservable<string>
  69. {
  70. public IDisposable Subscribe(IObserver<string> observer)
  71. {
  72. observer.OnNext("test");
  73. observer.OnNext("test");
  74. observer.OnNext("hallo");
  75. observer.OnNext("test");
  76. observer.OnNext("hallo");
  77. return Disposable.Empty;
  78. }
  79. }
Add Comment
Please, Sign In to add comment