1 /**
2 Contains a number of algorithms that operate on sequences. These sequences can be:
3
4 <li>$(LINK2 https://dlang.org/spec/template.html#variadic-templates, value sequences):
5 ---
6 assert(1.concat(2, 3, 4).array == [1, 2, 3, 4]);
7 ---
8 <li>$(LINK2 https://dlang.org/phobos/std_range_primitives.html, ranges):
9 ---
10 assert(1.concat([2, 3, 4]).array == [1, 2, 3, 4]);
11 ---
12 <li>a mixture of the above two:
13 ---
14 assert(1.concat([2, 3], 4).array == [1, 2, 3, 4]);
15 ---
16 <li>$(LINK2 https://dlang.org/spec/hash-map.html, associative arrays):
17 ---
18 auto aa = ["a": 1, "b": 0, "c": 2];
19 assert(aa.compactValues!(a => a == 0) == ["a": 1, "c": 2]);
20 ---
21
22 Furthermore, a number of algorithms allow you to:
23
24 <li>operate on members of types:
25
26 This would be akin to passing in a predicate that extracts a member variable from a type to
27 operate on instead of operating on the whole type. These algorithms usually have a `By` prefix:
28 ---
29 class C {
30 int x;
31 }
32 auto arr1 = [new C(2), new C(3)];
33 auto arr2 = [new C(2), new C(3)];
34 assert(arr1.equalBy!"x"(arr2));
35 ---
36 <li>operate via unary or binary predicates:
37 ---
38 import std.math: ceil;
39 assert([2.1, 1.2].difference!ceil([2.3, 3.4]).equal([1.2]));
40 assert([2.1, 1.2].difference!((a, b) => ceil(a) == ceil(b))([2.3, 3.4]).equal([1.2]));
41 ---
42 <li> or both:
43 ---
44 struct A {
45 int x;
46 }
47 auto arr = [A(4), A(8), A(12)];
48 assert(arr.pullBy!("x", a => a / 2)(5, 9).array == [A(12)]);
49 ---
50
51 Algorithms:
52
53 $(TABLE
54 $(TR $(TH Module) $(TH Functions) $(TH Properties) $(TH Description))
55 $(TR
56 $(TD $(DDOX_NAMED_REF ddash.algorithm.compact, `compact`))
57 $(TD
58 $(DDOX_NAMED_REF algorithm.compact.compact, `compact`)<br>
59 $(DDOX_NAMED_REF algorithm.compact.compactBy, `compactBy`)<br>
60 $(DDOX_NAMED_REF algorithm.compact.compactValues, `compactValues`)<br>
61 )
62 $(TD)
63 $(TD Creates a range or associative array with all null/predicate values removed.)
64 )
65 $(TR
66 $(TD $(DDOX_NAMED_REF ddash.algorithm.concat, `concat`))
67 $(TD
68 $(DDOX_NAMED_REF algorithm.concat.concat, `concat`)
69 )
70 $(TD)
71 $(TD Concatenates ranges and values together to a new range)
72 )
73 $(TR
74 $(TD $(DDOX_NAMED_REF ddash.algorithm.difference, `difference`))
75 $(TD
76 $(DDOX_NAMED_REF algorithm.difference.difference, `difference`)<br>
77 $(DDOX_NAMED_REF algorithm.difference.differenceBy, `differenceBy`)<br>
78 )
79 $(TD)
80 $(TD Creates a range of values not included in the other given set of values)
81 )
82 $(TR
83 $(TD $(DDOX_NAMED_REF ddash.algorithm.equal, `equal`))
84 $(TD
85 $(DDOX_NAMED_REF algorithm.equal.equal, `equal`)<br>
86 $(DDOX_NAMED_REF algorithm.equal.equalBy, `equalBy`)<br>
87 )
88 $(TD)
89 $(TD Tells you if two things are equal)
90 )
91 $(TR
92 $(TD $(DDOX_NAMED_REF ddash.algorithm.fill, `fill`))
93 $(TD
94 $(DDOX_NAMED_REF algorithm.fill.fill, `fill`)
95 )
96 $(TD mutates)
97 $(TD Assigns value to each element of input range.)
98 )
99 $(TR
100 $(TD $(DDOX_NAMED_REF ddash.algorithm.flatmap, `flatmap`))
101 $(TD
102 $(DDOX_NAMED_REF algorithm.flatmap.flatMap, `flatMap`)
103 )
104 $(TD)
105 $(TD Maps and flattens a range.)
106 )
107 $(TR
108 $(TD $(DDOX_NAMED_REF ddash.algorithm.flatten, `flatten`))
109 $(TD
110 $(DDOX_NAMED_REF algorithm.flatten.flatten, `flatten`)<br>
111 $(DDOX_NAMED_REF algorithm.flatten.flattenDeep, `flattenDeep`)<br>
112 )
113 $(TD)
114 $(TD Flattens a range by removing nesting levels values)
115 )
116 $(TR
117 $(TD $(DDOX_NAMED_REF ddash.algorithm.frompairs, `frompairs`))
118 $(TD
119 $(DDOX_NAMED_REF algorithm.frompairs.fromPairs, `fromPairs`)
120 )
121 $(TD)
122 $(TD Returns a newly allocated associative array from a range of key/value tuples)
123 )
124 $(TR
125 $(TD $(DDOX_NAMED_REF ddash.algorithm.index, `index`))
126 $(TD
127 $(DDOX_NAMED_REF algorithm.index.indexWhere, `indexWhere`)<br>
128 $(DDOX_NAMED_REF algorithm.index.lastIndexWhere, `lastIndexWhere`)<br>
129 $(DDOX_NAMED_REF algorithm.index.indexOf, `indexOf`)<br>
130 $(DDOX_NAMED_REF algorithm.index.lastIndexOf, `lastIndexOf`)<br>
131 )
132 $(TD)
133 $(TD Returns `optional` index of an element in a range.)
134 )
135 $(TR
136 $(TD $(DDOX_NAMED_REF ddash.algorithm.intersection, `intersection`))
137 $(TD
138 $(DDOX_NAMED_REF algorithm.intersection, `intersection`)
139 )
140 $(TD)
141 $(TD Creates a range of unique values that are included in the other given set of values)
142 )
143 $(TR
144 $(TD $(DDOX_NAMED_REF ddash.algorithm.pull, `pull`))
145 $(TD
146 $(DDOX_NAMED_REF algorithm.pull.pull, `pull`)<br>
147 $(DDOX_NAMED_REF algorithm.pull.pullAt, `pullAt`)<br>
148 $(DDOX_NAMED_REF algorithm.pull.pullBy, `pullBy`)<br>
149 )
150 $(TD)
151 $(TD Pulls elements out of a range)
152 )
153 $(TR
154 $(TD $(DDOX_NAMED_REF ddash.algorithm.remove, `remove`))
155 $(TD
156 $(DDOX_NAMED_REF algorithm.remove.remove, `remove`)
157 )
158 $(TD mutates)
159 $(TD Removed elements from a range by unary predicate)
160 )
161 $(TR
162 $(TD $(DDOX_NAMED_REF ddash.algorithm.reverse, `reverse`))
163 $(TD
164 $(DDOX_NAMED_REF algorithm.reverse.reverse, `reverse`)
165 )
166 $(TD mutates)
167 $(TD Reverses a range in place)
168 )
169 $(TR
170 $(TD $(DDOX_NAMED_REF ddash.algorithm.sort, `sort`))
171 $(TD
172 $(DDOX_NAMED_REF algorithm.sort.sortBy, `sortBy`)<br>
173 $(DDOX_NAMED_REF algorithm.sort.maybeSort, `maybeSort`)<br>
174 $(DDOX_NAMED_REF algorithm.sort.maybeSortBy, `maybeSortBy`)
175 )
176 $(TD)
177 $(TD Provides various ways for sorting a range)
178 )
179 $(TR
180 $(TD $(DDOX_NAMED_REF ddash.algorithm.stringify, `stringify`))
181 $(TD
182 $(DDOX_NAMED_REF algorithm.stringify.stringify, `stringify`)<br>
183 $(DDOX_NAMED_REF algorithm.stringify.stringifySeperatedBy, `stringifySeperatedBy`)<br>
184 )
185 $(TD)
186 $(TD Converts all elements in range into a string separated by separator.)
187 )
188 $(TR
189 $(TD $(DDOX_NAMED_REF ddash.algorithm.zip, `zip`))
190 $(TD
191 $(DDOX_NAMED_REF algorithm.zip.zipEach, `zipEach`)
192 )
193 $(TD)
194 $(TD Zips up ranges together)
195 )
196 )
197 */
198 module ddash.algorithm;
199
200 import ddash.common;
201
202 public {
203 import ddash.algorithm.flatmap;
204 import ddash.algorithm.compact;
205 import ddash.algorithm.concat;
206 import ddash.algorithm.difference;
207 import ddash.algorithm.equal;
208 import ddash.algorithm.fill;
209 import ddash.algorithm.flatmap;
210 import ddash.algorithm.flatten;
211 import ddash.algorithm.frompairs;
212 import ddash.algorithm.index;
213 import ddash.algorithm.intersection;
214 import ddash.algorithm.pull;
215 import ddash.algorithm.remove;
216 import ddash.algorithm.reverse;
217 import ddash.algorithm.sort;
218 import ddash.algorithm.stringify;
219 import ddash.algorithm.zip;
220 }