-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.coffee
805 lines (655 loc) · 26.4 KB
/
model.coffee
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
{helpers} = require('faraday')
fs = require('fs')
mongodb = require('mongodb')
ObjectID = mongodb.ObjectID
assert = require('assert')
step = require('step')
model = module.exports
_ = require('underscore')
helpers.setDebugTask('models', off)
Set = (() ->
#A helper class to hold connections to the database
ConnCollection = (value) ->
helpers.debugTask('models', "ConnCollection: I attempt to create a ConnCollection.")
result = Object.create(prototypeOfConnCollection)
result.value = value
helpers.debugTask('models', "ConnCollection: I create a ConnCollection: #{helpers.truncate(50, result)}.")
return result
prototypeOfConnCollection = {}
prototypeOfConnCollection.toString = () -> "[ConnCollection value: [#{this.value.map(String)}]]"
['push', 'pop', 'map', 'forEach', 'every', 'some'].forEach((k) -> #this MUST be forEach, NOT for..in; for..in does not properly scope; when k changes, I call the wrong method.
prototypeOfConnCollection[k] = () ->
Array.prototype[k].apply(this.value, arguments)
)
prototypeOfConnCollection.concat = (other) -> ConnCollection(@value.concat(other.value))
prototypeOfConnCollection.close = () ->
helpers.debugTask('models', "I close the connections: #{helpers.truncate(30, this)}.")
@forEach((conn) -> conn.close())
prototypeOfMongoDBCursorWrapper = {}
prototypeOfMongoDBCursorWrapper.next = (callback) ->
@value.nextObject((err, e) =>
assert.ifError(err)
if e is null
helpers.debugTask('models', "prototypeOfMongoDBCursorWrapper: I find no more documents!")
callback(undefined)
else
result = @set.create(e)
helpers.debugTask('models', "prototypeOfMongoDBCursorWrapper: I return the document (excluding functions) #{JSON.stringify(result)}.")
callback(result)
)
MongoDBCursorWrapper = (set, cursor) ->
helpers.debugTask('models', "MongoDBCursorWrapper: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
result = Object.create(prototypeOfMongoDBCursorWrapper)
result.value = cursor
result.set = set
return result
constructor = (nameParts) ->
helpers.debugTask('models', "constructor: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
result = Object.create(prototypeOfSet)
result.nameParts = nameParts
return result
prototypeOfSet = {}
prototypeOfSet.toString = () -> "[Set #{@dbName()}]"
prototypeOfSet.dbName = () -> @last.dbName() #The first set overrides this method.
prototypeOfSet.create = (attrs) -> @last.create(attrs) #The first set overrides this method.
prototypeOfSet.cursor = (f) ->
that = this
helpers.debugTask('models', "prototypeOfSet.cursor: I try to create the cursor for '#{@dbName()}'.")
helpers.mongoColl(@dbName(), ((conn, coll) ->
helpers.debugTask('models', "prototypeOfSet.cursor: I create the cursor with coll #{helpers.truncate(30, coll)}.")
f(ConnCollection([conn]), MongoDBCursorWrapper(that, coll.find({})))))
prototypeOfCursor = {}
prototypeOfCursor.toString = () -> "[Cursor #{@set.dbName()}]"
prototypeOfSet.where = (f) ->
helpers.debugTask('models', "prototypeOfSet.where: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
result = Object.create(prototypeOfWhereSet)
result.last = this
result.f = f
return result
prototypeOfWhereSet = Object.create(prototypeOfSet)
prototypeOfWhereSet.cursor = (callback) ->
helpers.debugTask('models', "prototypeOfWhereSet.cursor: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
that = this
@last.cursor((conns, cursor) ->
callback(conns, WhereCursor(that, cursor))
)
WhereCursor = (set, lastCursor) ->
helpers.debugTask('models', "WhereCursor: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
result = Object.create(prototypeOfWhereCursor)
result.last = lastCursor
result.set = set
return result
prototypeOfWhereCursor = Object.create(prototypeOfCursor)
prototypeOfWhereCursor.next = (callback) ->
helpers.debugTask('models', "prototypeOfWhereCursor.next: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
that = this
@last.next((e) ->
if e is undefined
callback(undefined)
else if that.set.f(e)
callback(e)
else
that.next(callback)
)
prototypeOfSet.map = (f) ->
helpers.debugTask('models', "prototypeOfSet.map: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
result = Object.create(prototypeOfMapSet)
result.f = f
result.last = this
return result
prototypeOfMapSet = Object.create(prototypeOfSet)
prototypeOfMapSet.cursor = (callback) ->
that = this
@last.cursor((conns, cursor) ->
callback(conns, MapCursor(that, cursor))
)
MapCursor = (set, lastCursor) ->
result = Object.create(prototypeOfMapCursor)
result.set = set
result.last = lastCursor
return result
prototypeOfMapCursor = Object.create(prototypeOfCursor)
prototypeOfMapCursor.next = (callback) ->
that = this
@last.next((e) ->
if e is undefined
callback(undefined)
else
callback(that.set.f(e))
)
prototypeOfSet.each = (f) ->
helpers.debugTask('models', "prototypeOfSet.each: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
result = Object.create(prototypeOfEachSet)
result.f = f
result.last = this
return result
prototypeOfEachSet = Object.create(prototypeOfSet)
prototypeOfEachSet.cursor = (callback) ->
that = this
@last.cursor((conns, cursor) ->
callback(conns, EachCursor(that, cursor))
)
EachCursor = (set, lastCursor) ->
result = Object.create(prototypeOfEachCursor)
result.set = set
result.last = lastCursor
return result
prototypeOfEachCursor = Object.create(prototypeOfCursor)
prototypeOfEachCursor.next = (callback) ->
@last.next((e) =>
if e is undefined
callback(undefined)
else
@set.f((() -> callback(e)), e)
)
helpers.setDebugTask('groupByKeyReduce', off)
prototypeOfSet.groupByKeyReduce = (elem2Key, elem2Initial, iterator) ->
result = Object.create(prototypeOfGroupByKeyReduceSet)
result.last = this
result.elem2Key = elem2Key
result.elem2Initial = elem2Initial
result.iterator = iterator
return result
prototypeOfGroupByKeyReduceSet = Object.create(prototypeOfSet)
prototypeOfGroupByKeyReduceSet.cursor = (callback) ->
@last.cursor((conns, cursor) =>
callback(conns, GroupByKeyReduceCursor(this, cursor))
)
GroupByKeyReduceCursor = (set, lastCursor) ->
result = Object.create(prototypeOfGroupByKeyReduceCursor)
result.set = set
result.last = lastCursor
result.hasLoaded = false
result.loaded = []
return result
prototypeOfGroupByKeyReduceCursor = Object.create(prototypeOfCursor)
prototypeOfGroupByKeyReduceCursor.loadAll = (callback) ->
helpers.debugTask('models', "prototypeOfGroupByKeyReduceCursor.loadAll: I am called.")
thiscursor = this
grouped = []
placeElem = (e) ->
key = thiscursor.set.elem2Key(e)
for [k, vs] in grouped
if key is k
vs.push(e)
return
grouped.push([key, [e]])
helpers.step((() ->
nextStep = this
eachElem = () ->
thiscursor.last.next((e) ->
if e is undefined
nextStep()
else
placeElem(e)
eachElem()
)
eachElem()
),(() ->
for [k, vs] in grouped
initial = thiscursor.set.elem2Initial(vs.pop())
reduced = vs.reduce(thiscursor.set.iterator, initial)
helpers.debugTask('groupByKeyReduce', "The time totals to #{reduced.value}.")
thiscursor.loaded.push(reduced)
thiscursor.hasLoaded = true
callback()
))
prototypeOfGroupByKeyReduceCursor.next = (callback) ->
if @loaded.length > 0
result = @loaded.pop()
callback(result)
else if @hasLoaded
callback(undefined)
else
@loadAll(() =>
@next(callback)
)
prototypeOfSet.cross = (other) ->
result = Object.create(prototypeOfCrossSet)
result.last = this
result.other = other
return result
prototypeOfCrossSet = Object.create(prototypeOfSet)
prototypeOfCrossSet.cursor = (callback) ->
return @last.cursor((lastConns, l) =>
@other.cursor((otherConns, o) =>
callback(lastConns.concat(otherConns),
CrossCursor(this, l, o))))
CrossCursor = (set, lastCursor, otherCursor) ->
result = Object.create(prototypeOfCrossCursor)
result.set = set
result.last = lastCursor
result.other = otherCursor
result.takeFrom = lastCursor
result.takeNextFrom = otherCursor
result.fromLast = []
result.fromOther = []
result.loaded = []
return result
prototypeOfCrossCursor = Object.create(prototypeOfCursor)
prototypeOfCrossCursor.firstElem = (callback) ->
@last.next((l) =>
@other.next((o) =>
if l is undefined or o is undefined
callback(undefined)
else
@fromLast.push(l)
@fromOther.push(o)
callback([l, o])
)
)
prototypeOfCrossCursor.onTakeFromNext = (e1, callback) ->
if @takeFrom is @last
@fromLast.push(e1)
for e2 in @fromOther
@loaded.push([e1, e2])
[@takeFrom, @takeNextFrom] = [@takeNextFrom, @takeFrom]
callback(@loaded.pop())
else if @takeFrom is @other
@fromOther.push(e1)
for e2 in @fromLast
@loaded.push([e2, e1])
[@takeFrom, @takeNextFrom] = [@takeNextFrom, @takeFrom]
callback(@loaded.pop())
else
throw "prototypeOfCrossCursor.next: @takeFrom is neither @last nor @other, but #{@takeFrom}!"
prototypeOfCrossCursor.next = (callback) ->
unless @fromLast.length > 0 or @fromOther.length > 0
@firstElem(callback)
else if @loaded.length > 0
result = @loaded.pop()
callback(result)
else
@takeFrom.next((e) =>
unless e is undefined
@onTakeFromNext(e, callback)
else
@takeFrom = @takeNextFrom #Now @takeFrom and @takeNextFrom are the same; both will never equal that @takeFrom just was.
@takeNextFrom.next((e) =>
unless e is undefined
@onTakeFromNext(e, callback)
else
callback(undefined)
)
)
prototypeOfSet.joinUsing = (keys, other) ->
return this.cross(other).where(([l, o]) ->
result = l._id.equals(o._id) and keys.every((key) -> l[key] is o[key])
return result
).map(([l, o]) ->
_.extend(o, l)
)
prototypeOfSet.join = (other) ->
return @joinUsing([], other)
prototypeOfSet.createAll = (list) -> CreateAllSet(this, list)
CreateAllSet = (last, arr) ->
result = Object.create(prototypeOfCreateAllSet)
result.last = last
result.newModels = arr.map(helpers.method(last, 'create'))
return result
prototypeOfCreateAllSet = Object.create(prototypeOfSet)
prototypeOfCreateAllSet.cursor = (callback) ->
that = this
@last.cursor((conns, cursor) ->
callback(conns, CreateAllCursor(that, cursor))
)
CreateAllCursor = (set, lastCursor) ->
result = Object.create(prototypeOfCreateAllCursor)
result.set = set
result.last = lastCursor
result.loaded = set.newModels
return result
prototypeOfCreateAllCursor = Object.create(prototypeOfCursor)
prototypeOfCreateAllCursor.next = (callback) ->
if @loaded.length > 0
callback(@loaded.pop())
else
@last.next(callback)
prototypeOfSet.invoke = (str, rest...) ->
helpers.debugTask('models', "prototypeOfSet.invoke: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
if rest.length is 1
args = []
callback = rest[0]
else
args = rest[0]
callback = rest[1]
immediateEach.apply(this,
[((callback, item) ->
item[str].apply(item, Array.prototype.concat.call(args, callback))
),
callback])
immediateEach = (f, callback) -> #Streamline by calling each element in paralell.
@cursor((conns, cursor) =>
eachElem = (e) =>
conns.close()
if e is undefined
callback()
else
f((() -> cursor.next(eachElem)), e)
cursor.next(eachElem)
)
prototypeOfSet.every = (cond, callback) ->
this.cursor((conns, cursor) ->
iter = () ->
cursor.next((e) =>
if e is undefined
conns.close()
callback(true)
else if not cond(e)
conns.close()
callback(false)
else
iter(conns, cursor, cond, callback)
)
iter()
)
prototypeOfSet.some = (cond, callback) ->
this.cursor((conns, cursor) ->
iter = () ->
cursor.next((e) =>
if e is undefined
conns.close()
callback(false)
else if cond(e)
conns.close()
callback(true)
else
iter(conns, cursor, cond, callback)
)
iter()
)
###
prototypeOfSet.reduce = (initial, f, callback) ->
this.cursor((conns, cursor) ->
current = initial
while cursor.hasNext()
current = f(current, cursor.next())
conns.close()
callback(current)
)
updateCursor = (coll, cursor, modifier, callback) ->
unless cursor.hasNext()
callback()
else
n = cursor.next()
oldID = n._id
coll.update({_id: oldID}, (() ->
updateCursor(coll, cursor, modifier, callback)
))
prototypeOfSet.update = (modifier, callback) ->
that = this
step((() ->
helpers.mongoColl(that.dbName(), this)
),((conn, coll) ->
that.last.cursor((conns, cursor) ->
updateCursor(coll.find(), cursor, modifier, (() -> this(conns.concat([conn]))))
)
),((conns) ->
conns.close()
callback()
))
###
prototypeOfSet.save = (callback) ->
helpers.debugTask('models', "prototypeOfSet.save: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
this.invoke('save', callback)
prototypeOfSet.destroy = (callback) ->
helpers.debugTask('models', "prototypeOfSet.destroy: I have been called with #{helpers.truncate(String(Array.prototype.map.call(arguments, String)))}.")
this.invoke('destroy', callback)
prototypeOfSet.count = (callback) ->
@cursor((conns, cursor) ->
onNext = (e, callback) ->
if e is undefined
conns.close()
callback(0)
else
cursor.next((e) -> onNext(e, (n) -> callback(1 + n)))
cursor.next((e) ->
onNext(e, callback)
)
)
prototypeOfSet.toArray = (callback) ->
result = []
@cursor((conns, cursor) ->
cursor.next((e) ->
if e is undefined
conns.close()
callback(result)
else
result.push(e)
cursor.next(arguments.callee)
)
)
prototypeOfSet.one = (callback) ->
@cursor((conns, cursor) ->
cursor.next((e) ->
conns.close()
callback(e)
)
)
prototypeOfSet.then = (callback) ->
@cursor((conns, cursor) ->
cursor.next((result) ->
if result is undefined
conns.close()
callback()
else
cursor.next(arguments.callee)
)
)
return constructor
)()
prototypeOfModel = {}
prototypeOfModel.save = (callback) ->
@_id ?= ObjectID()
helpers.mongoColl(@dbName, ((conn, coll) =>
coll.save(this, {safe: true}, (() =>
conn.close()
callback()
))
))
prototypeOfModel.destroy = (callback) ->
if not @_id
callback()
helpers.mongoColl(@dbName, ((conn, coll) =>
coll.remove({_id: @_id}, {w: 2}, ((err, n) ->
assert.ifError(err)
assert(n is 1)
conn.close()
callback()
))
))
Model = (dbName) ->
result = Object.create(prototypeOfModel)
result.dbName = dbName
return result
namePart = (dir) ->
m = dir.match(/\w+(?=\.\w+$)/)
assert(m)
return m[0]
nameParts = (dir) ->
result = dir.split('/')
result[result.length - 1] = namePart(result[result.length - 1])
return result
helpers.forEachVisibleFileSync('./models', ((partdir) ->
modelDir = '../.' + partdir #this is only the dir relative to the main module. We need to get there with '../.'.
modelNameParts = nameParts(partdir[2..])[1..]
dbName = modelNameParts.join('.')
set = Set(modelNameParts)
set.dbName = () -> dbName
prototypeOfNewModel = Model(dbName)
newSet = require(modelDir)(prototypeOfNewModel, set)
oldSet = Set(modelNameParts)
oldSet.dbName = () -> dbName
for k, v in oldSet
unless newSet[k] is v
helpers.warnStamped("You have altered [#{modelNameParts.join('.')} Set]##{k}.")
if newSet.create is oldSet.create
newSet.create = (obj) ->
result = Object.create(prototypeOfNewModel)
for k, v of obj
result[k] = v
return result
helpers.setRecursive(module.exports, modelNameParts, newSet)
assert(helpers.getRecursive(module.exports, modelNameParts) is newSet)))
###
WARNING: Some methods have callbacks. When they invoke the callback, they do not gurantee that the db connection is closed.
where((entry) -> Bool)
groupBy((a, b) -> Bool) --unimplemented
groupByKey((entry) -> key) --unimplemented
groupByKeyReduce((entry) -> key, (key) -> initial, (last, entry) -> next)
plus/concat(another_iterator) --unimplemented
minus/without(another_iterator) --unimplemented
cross(another_iterator)
join(another_iterator) --unimplemented
joinUsing([key1, key2..])
intersect(another_iterator) --unimplemented
map((item) -> newItem)
pluck(propName) --unimplemented
union(another_iterator) --unimplemented
intersectAll(another_iterator) --unimplemented
limit(Number) --unimplemented
find(attrs) --unimplemented
contains(elem) --unimplemented
uniq() --unimplemented
insert(item) --unimplemented
insertAll(items) --unimplemented
one() #selects a random model
some((entry) -> Bool, callback)
every((entry) -> Bool, callback)
reduce(initial, (acc, entry) -> newAcc, callback)
invoke(func or funcName)
each((next, item) -> ... item.save?, callback)
destroy(callback)
removeMongo(object_selector, callback) --unimplemented
update((item) -> newItem, callback)
save(callback)
toArray(callback)
count(callback)
then(callback)
model.toSet
obj = model.Task.create(args)
model.Task.where((item) -> item._id is 148912)
model.Task.where((a) -> model.Task.every((b) -> b._id < a._id)).destroy(callback)
myTask = model.Task.create('Run')
myTask.save(() ->
#moreStuff...
))
myTask.refresh((newTask) ->
newTask.f = (x) -> x
newTask.save(() ->
#moreStuff...
))
))
###
###
prototypeOfSet.insert = (item, callback) ->
that = this
step((() ->
helpers.mongoColl(that.name(), this)
), ((conn, coll) ->
coll.insert(item, {safe: true}, this)
), ((err, inserted) ->
assert.ifError(err)
conn.close()
callback(inserted)))
###
###
stripExtension = (str) ->
m = str.match(/^\w+/)
unless m
throw "stripExtension: I cannot strip the extension from '#{str}'."
return m[0]
###
###
prototypeOfSet.name = () ->
assert(@name or @last, "prototypeOfSet.name: #{this} has neither a name nor a last Set.")
return @name or @last.name()
###
###
prototypeOfCursor.next = () ->
helpers.debugTask('models', "prototypeOfCursor.next: I attempt to return the next item.")
if @hasNext()
i = @loaded.pop()
helpers.debugTask('models', "prototypeOfCursor.next: I return the item #{helpers.truncate(i)}.")
return i
else
throw StopIteration
###
###
prototypeOfCreateAllCursor.hasNext = () ->
if @loaded.length > 0
return true
else if @last.hasNext()
@loaded.push(@last.next())
return true
else
return false
###
###
#I define set.create twice to resolve circular dependencies when the model calls all models.
set.create = () -> require(modelDir)(Model(set.dbName())).apply({}, arguments)
modelSet = require(modelDir)(Model(set.dbName()))
set.create = () -> modelConstructor.apply({}, arguments)
###
###
else unless @last.hasNext() or @other.hasNext()
return false
else
if not @last.hasNext()
@takeFrom = @other
else if not @other.hasNext()
@takeFrom = @last
if @takeFrom is @last
n = @last.next()
@fromOther.forEach((item) ->
@loaded.push([n, item])
)
@takeFrom = @other
else if @takeFrom is @other
n = @other.next()
@fromLast.forEach((item) ->
@loaded.push([item, n])
)
@takeFrom = @last
return true
###
###
prototypeOfSet.groupByReduce = (last, areSimilar, entry2Initial, reduce) ->
result = Object.create(prototypeOfGroupByReduceSet)
result.last = last
result.areSimilar = areSimilar
result.entry2Initial = entry2Initial
result.reduce = reduce
return result
prototypeOfGroupByReduceSet = Object.create(prototypeOfSet)
prototypeOfGroupByReduceSet.cursor = (callback) ->
return callback(@last.conns, GroupByReduceCursor(this, @last.cursor))
GroupByReduceCursor = (set, lastCursor) ->
result = Object.create(prototypeOfGroupByReduceCursor)
result.set = set
result.last = lastCursor
prototypeOfGroupByReduceCursor = Object.create(prototypeOfCursor)
prototypeOfGroupByReduceCursor.hasNext = (() ->
result = () ->
if @loaded
return @loaded.length > 0
else
@loaded = placeInGroups(@set.areSimilar, lastCursor).map((group) ->
initial = @set.entry2Initial(group[0])
return group.reduce(initial, @set.reduce)
)
placeInGroups = (areSimilar, cursor) ->
result = []
while cursor.hasNext()
result = placeInGroup(cursor.next(), result, areSimilar)
return result
placeInGroup = (item, arr, areSimilar) ->
if arr.length is 0
return [[item]]
else if areSimilar(item, arr[0][0])
return [arr[0].concat([item])].concat(arr.slice(1))
else
return [arr[0]].concat(placeInGroup(item, arr.slice(1), areSimilar))
return result
)()
###