-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.d
104 lines (91 loc) · 2.7 KB
/
zip.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
module uim.collections.classes.iterators.zip;
import uim.collections;
@safe:
/**
* Creates an iterator that returns elements grouped in pairs
*
* ### Example
*
* ```
* anIterator = new DZipIterator([[1, 2], [3, 4]]);
* anIterator.toList(); // Returns [[1, 3], [2, 4]]
* ```
*
* You can also chose a custom auto to zip the elements together, such
* as doing a sum by index:
*
* ### Example
*
* ```
* anIterator = new DZipIterator([[1, 2], [3, 4]], auto (a, b) {
* return a + b;
* });
* anIterator.toList(); // Returns [4, 6]
* ```
*/
class DZipIterator : ICollection {
/*
mixin TCollection;
protected DMultipleIterator multipleIterator;
// The auto to use for zipping items together
protected callable _callback;
// Contains the original iterator objects that were attached
// TODO protected array _iterators = null;
/**
* Creates the iterator to merge together the values by for all the passed
* iterators by their corresponding index.
*
* @param array sets The list of array or iterators to be zipped.
* @param callable|null aCallable The auto to use for zipping the elements of each iterator.
* /
this(array sets, ?callable aCallable = null) {
_multipleIterator = new DMultipleIterator(
MultipleIterator.MIT_NEED_ALL | MultipleIterator.MIT_KEYS_NUMERIC
);
_callback = aCallable;
sets.each!((set) {
anIterator = (new DCollection(set)).unwrap();
_iterators ~= anIterator;
_multipleIterator.attachIterator(anIterator);
});
}
/**
* Returns the value resulting out of zipping all the elements for all the
* iterators with the same positional index.
* /
Json current() {
current = _multipleIterator.current();
if (_callback) {
return call_user_func_array(_callback, current);
}
return current;
}
Json key() {
return _multipleIterator.key();
}
void next() {
_multipleIterator.next();
}
void rewind()) {
_multipleIterator.rewind();
}
bool valid() {
return _multipleIterator.valid();
}
// Magic method used for serializing the iterator instance.
array __serialize() {
return _iterators;
}
/**
* Magic method used to rebuild the iterator instance.
* Params:
* array data Data array.
* /
void __unserialize(array data) {
_multipleIterator = new DMultipleIterator(
MultipleIterator.MIT_NEED_ALL | MultipleIterator.MIT_KEYS_NUMERIC
);
_iterators = someData;
_iterators.each!(iterator => _multipleIterator.attachIterator(iterator));
} */
}