zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>linq.js tutorial</title>
    <script type="text/javascript" src="../linq.js"></script>
    <script type="text/javascript">
        var textStack = []
        document.write = function ()
        {
            for (var i = 0; i < arguments.length; i++)
            {
                textStack.push(arguments[i]);
            }
        }
 
        window.onload = function ()
        {
            Enumerable.From(document.getElementsByTagName("pre"))
                .ForEach(function (ele)
                {
                    eval(ele.innerText || ele.textContent);
                    var p = document.createElement("p");
                    p.innerHTML = textStack.join("");
                    ele.appendChild(p);
                    textStack.length = 0;
                });
        }
    </script>
</head>
<body>
    <h2>
        First step of Lambda Expression</h2>
    <pre>
    // Anonymous function
    Enumerable.Range(1, 3).Select(function(value, index) { return index + ':' + value }).WriteLine();
    // String like Lambda Expression (arguments => expression)
    Enumerable.Range(1, 3).Select("value,index=>index+':'+value").WriteLine();
    
    // If the number of arguments is one , can use default iterator variable '$'
    Enumerable.Range(1, 3).Select("i=>i*2").WriteLine();
    Enumerable.Range(1, 3).Select("$*2").WriteLine(); // same
    
    // "" is shortcut of "x => x" (identity function)
    Enumerable.Range(4, 7).Join(Enumerable.Range(8, 5), "", "", "outer,inner=>outer*inner").WriteLine();
    </pre>
    <h2>
        Scope of lambda expression</h2>
    <pre>
    var number = 3;
    // Can't Find number | lambda expression can use only global variable
    // Enumerable.Range(1,10).Where("$ == number").WriteLine();
 
    // use anonymous founction, can capture variable
    Enumerable.Range(1,10).Where(function(i){return i == number}).WriteLine();
    </pre>
    <h2>
        From(Object) -> convert to KeyValuePair</h2>
    <pre>
    var object = {foo:"a", "bar":100, "foobar":true};
    Enumerable.From(object).ForEach(function(obj)
    {
        document.write(obj.Key + ":" + obj.Value + "&lt;br />");
    })
    </pre>
    <h2>
        ForEach (continue and break)</h2>
    <pre>
    Enumerable.Repeat("foo", 10).ForEach(function(value, index)
    {
        if (index % 2 == 0) return; // continue
        if (index > 6) return false; // break
        document.write(index + ":" + value + "&lt;br />");
    });
    </pre>
    <h2>
        Grouping and ref/value compare</h2>
    <pre>
    // ref compare
    document.write((new Date(2000, 1, 1) == new Date(2000, 1, 1)) + "&lt;br />"); // false
    document.write(({ a: 0} == { a: 0 }) + "&lt;br />"); // false
    
    document.write("------" + "&lt;br />");
    var objects = [
        { Date: new Date(2000, 1, 1), Id: 1 },
        { Date: new Date(2010, 5, 5), Id: 2 },
        { Date: new Date(2000, 1, 1), Id: 3 }
    ]
 
    // ref compare, can not grouping
    Enumerable.From(objects)
        .GroupBy("$.Date", "$.Id",
            function (key, group) { return { date: key, ids: group.ToString(',')} })
        .WriteLine("$.date + ':' + $.ids");
 
    document.write("------" + "&lt;br />");
 
    // use fourth argument(compareSelector)
    Enumerable.From(objects)
        .GroupBy("$.Date", "$.Id",
            function (key, group) { return { date: key, ids: group.ToString(',')} },
            function (key) { return key.toString() })
        .WriteLine("$.date + ':' + $.ids");
        </pre>
    <h2>
        Regular Expression Matches</h2>
    <pre>
    // Enumerable.Matches return Enumerable&lt;MatchObject>
 
    var input = "abcdefgABzDefabgdg";
    Enumerable.Matches(input, "ab(.)d", "i").ForEach(function(match)
    {
        for (var prop in match)
        {
            document.write(prop + " : " + match[prop] + "&lt;br />");
        }
        document.write("toString() : " + match.toString() + "&lt;br />");
        document.write("---" + "&lt;br />");
    });
    </pre>
    <h2>
        LazyEvaluation and InfinityList</h2>
    <pre>
    // first radius of circle's area over 10000
    var result = Enumerable.ToInfinity(1).Where("r=>r*r*Math.PI>10000").First();
    document.write(result);
    </pre>
    <h2>
        Dictionary</h2>
    <pre>
    // sample class
    var cls = function (a, b)
    {
        this.a = a;
        this.b = b;
    }
    var instanceA = new cls("a", 100);
    var instanceB = new cls("b", 2000);
 
    // create blank dictionary
    var dict = Enumerable.Empty().ToDictionary();
    // create blank dictionary(use compareSelector)
    var dict = Enumerable.Empty().ToDictionary("","",function (x) { return x.a + x.b });
 
    dict.Add(instanceA, "zzz");
    dict.Add(instanceB, "huga");
    document.write(dict.Get(instanceA) + "&lt;br />"); // zzz
    document.write(dict.Get(instanceB) + "&lt;br />"); // huga
 
    // enumerable (to KeyValuePair)
    dict.ToEnumerable().ForEach(function (kvp)
    {
        document.write(kvp.Key.a + ":" + kvp.Value + "&lt;br />");
    });
    </pre>
    <h2>
        sample - Nondeterministic Programs</h2>
    // from Structure and Interpretation of Computer Programs 4.3.2
    <pre>
    var apart = Enumerable.Range(1, 5);
    var answers = apart
        .SelectMany(function(baker){ return apart
        .SelectMany(function(cooper){ return apart
        .SelectMany(function(fletcher){ return apart
        .SelectMany(function(miller){ return apart
        .Select(function(smith){ return {
            baker: baker, cooper: cooper, fletcher: fletcher, miller: miller, smith: smith}})})})})})
        .Where("Enumerable.From($).Distinct('$.Value').Count() == 5")
        .Where("$.baker != 5")
        .Where("$.cooper != 1")
        .Where("$.fletcher != 1 && $.fletcher != 5")
        .Where("$.miller > $.cooper")
        .Where("Math.abs($.smith - $.fletcher) != 1")
        .Where("Math.abs($.fletcher - $.cooper) != 1");
 
    answers.SelectMany("").WriteLine("$.Key + ':' + $.Value");
    </pre>
</body>
</html>