-
Notifications
You must be signed in to change notification settings - Fork 1
/
restriction.d
73 lines (60 loc) · 1.57 KB
/
restriction.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
import std.algorithm;
import std.array;
import std.stdio;
import std.range;
import data;
/** $(BIG Restriction Operators) */
/** Where - Simple 1
* This sample uses where to find all elements of an array less than 5.
*/
auto linq1()
{
auto numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0];
return numbers.filter!(a => a < 5);
}
/**Where - Simple 2
* This sample uses where to find all products that are out of stock.
*/
auto linq2()
{
auto products = getProducts();
return products.filter!(a => a.unitsInStock == 0).array;
}
/** Where - Simple 3
* This sample uses where to find all products that are in stock and cost more than 3.00 per unit.
*/
auto linq3()
{
auto products = getProducts();
return products.filter!(a => a.unitsInStock > 0 && a.unitPrice > 3.0).array;
}
/** Where - Drilldown
* This sample uses where to find all customers in Washington and then uses the resulting sequence to drill down into their orders.
*/
auto linq4()
{
auto customers = getCustomerList();
return customers.filter!(c => c.region == "WA");
}
/** Where - Indexed
* This sample demonstrates an indexed Where clause that returns digits whose name is shorter than their value.
*/
auto linq5()
{
string[] digits = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
];
return digits.enumerate.filter!(e => e.value.length < e.index).map!(e => e.value);
}
unittest
{
assert(linq1() == [4, 1, 3, 2, 0]);
}
unittest
{
assert(linq2().length == 5);
}
unittest
{
assert(linq5().equal(["five", "six", "seven", "eight", "nine"]));
}