deno.land / x / linq@4.0.2 / sample / tutorial.js

نووسراو ببینە
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
import Enumerable from '../linq.js'
/////////////////////////////// Simple Lambda Expressions
console.log('# Simple Lambda Expressions\n');
// Anonymous functionEnumerable.range(1, 3).select(function(value, index) { return index + ':' + value }).log().toJoinedString();
// Arrow function syntaxEnumerable.range(1, 3).select((value, index) => index + ':' + value).log().toJoinedString();
// Lambdas can also be passed as stringsEnumerable.range(1, 3).select("(value, index) => index + '/' + value").log().toJoinedString();
// If the number of arguments is one, we can use default iterator variable '$'Enumerable.range(1, 3).select("$*2").log().toJoinedString(); // same as i => i * 2
// "" is a shortcut for "x => x" (identity function)Enumerable.range(4, 7).join(Enumerable.range(8, 5), "", "", (outer, inner) => outer * inner).log().toJoinedString();

///////////////////////////////// Scope of lambda expressions
console.log('\n# Scope of lambda expressions\n');
// lambda expressions cannot access the closure when the string syntax is usedtry { var number = 3; Enumerable.range(1,10).where("$ == number").log().toJoinedString();} catch (e) { console.log("can't find number");}
// use anonymous function to capture variableEnumerable.range(1,10).where(function(i){return i == number}).log().toJoinedString();

/////////////////////////////// Initializing from objects
console.log('\n# Initializing from objects\n');
var object = {foo:"a", "bar":100, "foobar":true};Enumerable.from(object).forEach(function(obj) { console.log(obj.key + ":" + obj.value) });

/////////////////////////////////////// Continue and break when iterating
console.log('\n# Continue and break when iterating\n');
Enumerable.repeat("foo", 10).forEach(function(value, index){ if (index % 2 == 0) return; // continue if (index > 6) return false; // break console.log(index + ":" + value);});

//////////////////////////////////// Grouping and ref/value compare
console.log('\n# Grouping and ref/value compare\n');
// ref compareconsole.log((new Date(2000, 1, 1) == new Date(2000, 1, 1))); // falseconsole.log(({ a: 0} == { a: 0 })); // false
console.log("------");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, cannot do groupingEnumerable.from(objects) .groupBy("$.Date", "$.Id", function (key, group) { return { date: key, ids: group.toJoinedString(',')} }) .log("$.date + ':' + $.ids").toJoinedString();
console.log("------");
// use fourth argument to groupBy (compareSelector)Enumerable.from(objects) .groupBy("$.Date", "$.Id", function (key, group) { return { date: key, ids: group.toJoinedString(',')} }, function (key) { return key.toString() }) .log("$.date + ':' + $.ids").toJoinedString();

//////////////////////////////// Regular Expression matches
console.log('\n# Regular Expression matches\n');
// Enumerable.matches return Enumerable<MatchObject>
var input = "abcdefgABzDefabgdg";Enumerable.matches(input, "ab(.)d", "i").forEach(function(match){ for (var prop in match) { console.log(prop + " : " + match[prop]); } console.log("toString() : " + match.toString()); console.log("---");});

///////////////////////////////////// LazyEvaluation and InfinityList
console.log('\n# LazyEvaluation and InfinityList\n');
// first radius of a circle with area over 10000var result = Enumerable.toInfinity(1).where(r => r * r * Math.PI > 10000).first();console.log(result);

//////////////// Dictionary
console.log('\n# Dictionary\n');
// sample classvar cls = function (a, b){ this.a = a; this.b = b;}var instanceA = new cls("a", 100);var instanceB = new cls("b", 2000);
// create blank dictionaryvar dict = Enumerable.empty().toDictionary();// create blank dictionary (using compareSelector)var dict = Enumerable.empty().toDictionary("","",function (x) { return x.a + x.b });
dict.add(instanceA, "zzz");dict.add(instanceB, "huga");console.log(dict.get(instanceA)); // zzzconsole.log(dict.get(instanceB)); // huga
// enumerable (to keyvaluePair)dict.toEnumerable().forEach(function (kvp){ console.log(kvp.key.a + ":" + kvp.value);});

/////////////////////////////// Nondeterministic Programs
console.log('\n# Nondeterministic programs\n');
// a puzzle from Structure and Interpretation of Computer Programs// http://sarabander.github.io/sicp/html/4_002e3.xhtml
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(function(x){ return Enumerable.from(x).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("").log("$.key + ':' + $.value").toJoinedString();
linq

Version Info

Tagged at
7 months ago