-
Notifications
You must be signed in to change notification settings - Fork 18
/
WordsPerMinute.cs
49 lines (38 loc) · 1.74 KB
/
WordsPerMinute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Reactive.Linq;
namespace Timed;
public static class WordsPerMinute
{
private static readonly KeyWatcher keySource = new();
private static readonly IObservable<IList<char>> wordWindows = keySource.Buffer(
() => keySource.FirstAsync(char.IsWhiteSpace));
private static readonly IObservable<string> words = from wordWindow in wordWindows
select new string(wordWindow.ToArray()).Trim();
public static void WithGroups()
{
IObservable<long> ticks = Observable.Interval(TimeSpan.FromSeconds(6));
IObservable<int> wordGroupCounts = from tick in ticks
join word in words
on ticks equals words into wordsInTick
from count in wordsInTick.Count()
select count * 10;
wordGroupCounts.Subscribe(c => Console.WriteLine($"Words per minute: {c}"));
keySource.Run();
}
public static void TimedWindowsWithBuffer()
{
IObservable<int> wordGroupCounts =
from wordGroup in words.Buffer(TimeSpan.FromSeconds(6))
select wordGroup.Count * 10;
wordGroupCounts.Subscribe(c => Console.WriteLine("Words per minute: " + c));
keySource.Run();
}
public static void OverlappingTimedWindows()
{
IObservable<int> wordGroupCounts =
from wordGroup in words.Buffer(TimeSpan.FromSeconds(6),
TimeSpan.FromSeconds(1))
select wordGroup.Count * 10;
wordGroupCounts.Subscribe(c => Console.WriteLine("Words per minute: " + c));
keySource.Run();
}
}