-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperatorSamples.cs
209 lines (166 loc) · 4.35 KB
/
OperatorSamples.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Deferred Execution 1:
*
* Demonstrate that execution occurs when a collection is enumerated, *not*
* when the expression is evaluated, which is sometimes really useful!
*/
var thisShouldTakeALongTime = Enumerable.Range(0, 1000 * 1000).Select(x => {
Thread.Sleep(1000);
return x * 10;
});
Console.WriteLine("The 1st number is " + thisShouldTakeALongTime.First());
The 1st number is 0
/* Deferred Execution 2:
*
* Sometimes deferred execution *isn't* useful, let's see how deferred
* execution could cause us to have unexpected results:
*/
int counter = 0;
var evenNumbersInSeries =
Enumerable.Range(0, 10).Select(
x =>
{
int result = x + counter;
counter++;
return result;
});
// List the numbers in the series
Console.WriteLine("First Try:\n");
foreach(int i in evenNumbersInSeries)
{
Console.WriteLine(i);
}
// We're running the same code again here, we'll certainly get the same
// result, right?
Console.WriteLine("\nSecond Try:\n");
foreach(int i in evenNumbersInSeries)
{
Console.WriteLine(i);
}
First Try:
0
2
4
6
8
10
12
14
16
18
Second Try:
10
12
14
16
18
20
22
24
26
28
/* Deferred Execution 3:
*
* We can fix the broken code above by adding a ToArray operator to the
* pipeline.
* this turns the evenNumbersInSeries from an IEnumerable to an array and the array is populated before
* the values are displayed
*/
int counter = 0;
var evenNumbersInSeries = Enumerable.Range(0, 10).Select(x => {
int result = x + counter;
counter++;
return result;
}).ToArray();
// List the numbers in the series
Console.WriteLine("First Try:\n");
foreach(int i in evenNumbersInSeries) {
Console.WriteLine(i);
}
// This time, because we added the ToArray(), we'll get the expected result
// every time.
Console.WriteLine("\nSecond Try:\n");
foreach(int i in evenNumbersInSeries) {
Console.WriteLine(i);
}
First Try:
0
2
4
6
8
10
12
14
16
18
Second Try:
0
2
4
6
8
10
12
14
16
18
/* Take 1:
*
* Return a subset of the items in the collection.
*/
var input = new[] {1,2,3,4,5,4,3,2,1};
var output = input.Take(5).Select(x => x * 10);
output.Dump();
10
20
30
40
50
/* Distinct 1:
*
* Distinct returns the set of unique items in a collection, removing any
* duplicate items.
*/
var input = new[] {1,2,3,2,1,2,3,2,1,2,3,2,1};
input.Distinct().Dump();
1
2
3
/* SelectMany 1:
*
* SelectMany lets you expand each item into either zero, one, or many items.
* This is one of the more difficult operators to understand, but also one of
* the most powerful.
*
* One way to think of how this operator works, is that it "flattens" a list
* of lists - so if we have [[1,2,3], [4], [5, 6]], the result will be
* [1,2,3,4,5,6]. This is easier to understand, but also hides some of
* SelectMany's interesting uses. It's often better to think of SelectMany as,
* "For each item in this list, I can replace it with whatever I want -
* nothing, a single item, or another list".
*
* This method will be even more useful for us when we look at the Reactive
* version of it, where it is instrumental to helping us chain calls to
* asynchronous methods (i.e. call 'A', pass the result to 'B', pass its
* result to 'C', etc).
*
* In this example, we will write a recursive method that finds all of the
* files in a folder.
*/
IEnumerable<string> GetFilesInAllSubdirectories(string root)
{
var di = new System.IO.DirectoryInfo(root);
// This line is the interesting one to grok - we are taking the stream of
// all directories in the current folder, and for each one, expanding it
// into all of the files in that directory.
return di.GetDirectories()
.SelectMany(x => GetFilesInAllSubdirectories(x.FullName))
.Concat(di.GetFiles().Select(x => x.FullName));
}
void Main()
{
var allFilesOnDesktop = GetFilesInAllSubdirectories(
System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
allFilesOnDesktop.Dump();
}
// vim: ts=4 sw=4 tw=80 et :