-
Notifications
You must be signed in to change notification settings - Fork 1
/
projection.d
198 lines (160 loc) · 5.61 KB
/
projection.d
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
/** Projection Operators */
import std.algorithm;
import std.array;
import std.datetime;
import std.format;
import std.range;
import std.typecons;
import std.uni;
import data;
/** Select - Simple 1
* This sample uses select to produce a sequence of ints one higher than those in an existing array of ints.
*/
auto linq6()
{
immutable numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0];
return map!(a => a + 1)(numbers).array;
}
/** Select - Simple 2
* This sample uses select to return a sequence of just the names of a list of products.
*/
auto linq7()
{
Product[] products = getProducts();
return map!(a => a.productName)(products).array;
}
/** Select - Transformation
* This sample uses select to produce a sequence of strings representing the text version of a sequence of ints.
*/
auto linq8()
{
int[] numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0];
string[] strings = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
];
return map!(a => strings[a])(numbers).array;
}
/** Select - Anonymous Types 1
* This sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array.
*/
auto linq9()
{
string[] words = ["aPPLE", "BlUeBeRrY", "cHeRry"];
return map!(a => tuple(a.toUpper(), a.toLower()))(words);
}
/** Select - Anonymous Types 2
* This sample uses select to produce a sequence containing text representations of digits and whether their length is even or odd.
*/
auto linq10()
{
int[] numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0];
string[] strings = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
];
return map!(a => tuple(strings[a], a % 2 == 0))(numbers);
}
/** Select - Anonymous Types 3
* This sample uses select to produce a sequence containing some properties of Products, including UnitPrice which is renamed to Price in the resulting type.
*/
auto linq11()
{
Product[] products = getProducts();
// this is not exaclty "anonymous type", is it :)
alias ProductInfo = Tuple!(string, "productName", string, "category", double, "unitPrice");
return map!(a => ProductInfo(a.productName, a.category, a.unitPrice))(products);
}
/** Select - Indexed
* This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array.
*/
auto linq12()
{
int[] numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0];
return zip(numbers, numbers.enumerate.map!(a => a.value == numbers[a.value]));
}
/** Select - Filtered
* This sample combines select and where to make a simple query that returns the text form of each digit less than 5.
*/
auto linq13()
{
int[] numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0];
string[] digits = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
];
return map!(a => digits[a])(filter!(a => a < 5)(numbers));
}
/** SelectMany - Compound from 1
* This sample uses a compound from clause to make a query that returns all pairs of numbers from both arrays such
* that the number from numbersA is less than the number from numbersB.
*/
auto linq14()
{
int[] numbersA = [0, 2, 4, 5, 6, 8, 9];
int[] numbersB = [1, 3, 5, 7, 8];
return filter!(a => a[0] < a[1])(zip(numbersA, numbersB));
}
/** SelectMany - Compound from 2
* This sample uses a compound from clause to select all orders where the order total is less than 500.00.
*/
auto linq15()
{
auto customers = getCustomerList();
return customers.map!(c => c.orders.filter!(it => it.total < 500.0)
.map!(x => tuple(c.customerID, x.orderID, x.total))).joiner();
}
/** SelectMany - Compound from 3
* This sample uses a compound from clause to select all orders where the order was made in 1998 or later.
*/
auto linq16()
{
auto customers = getCustomerList();
return customers.map!(c => c.orders.filter!(it => it.orderDate > DateTime(1998,
1, 1)).map!(x => tuple(c.customerID, x.orderID, x.orderDate))).joiner();
}
/** SelectMany - from Assignment
* This sample uses a compound from clause to select all orders where the order total is greater than 2000.00 and uses from assignment to avoid requesting the total twice
*/
auto linq17()
{
auto customers = getCustomerList();
return customers.map!(c => c.orders.filter!(it => it.total >= 2000.0)
.map!(x => tuple(c.customerID, x.orderID, x.total))).joiner();
}
/** SelectMany - Multiple from
* This sample uses multiple from clauses so that filtering on customers can be done
* before selecting their orders. This makes the query more efficient by not selecting
* and then discarding orders for customers outside of Washington.
*/
auto linq18()
{
auto customers = getCustomerList();
auto cutoffDate = DateTime(1997, 1, 1);
return customers.filter!(c => c.region == "WA")
.map!(c => c.orders.filter!(it => it.orderDate >= cutoffDate)
.map!(x => tuple(c.customerID, x.orderID))).joiner();
}
/** SelectMany - Indexed
* This sample uses an indexed SelectMany clause to select all orders,
* while referring to customers by the order in which they are returned from the query.
*/
auto linq19()
{
auto customers = getCustomerList();
return customers.enumerate.map!(e => tuple(e.value, e.index)).map!(cc => cc[0].orders.map!(
o => format!"Customer #%d has an order with OrderID %d"(cc[1] + 1, o.orderID))).joiner();
}
unittest
{
assert(equal(linq6(), [6, 5, 2, 4, 10, 9, 7, 8, 3, 1]));
}
unittest
{
assert(linq7().length == 77);
}
unittest
{
assert(equal(linq8(), ["five", "four", "one", "three", "nine", "eight",
"six", "seven", "two", "zero"]));
}
unittest
{
}