| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.algorithm.iteration; |
| 4 |
|
| 5 |
import std.algorithm.comparison : equal; |
| 6 |
import std.range, std.stdio; |
| 7 |
import std.typecons : tuple; |
| 8 |
|
| 9 |
ulong counter = 0; |
| 10 |
double fun(int x) |
| 11 |
{ |
| 12 |
++counter; |
| 13 |
// http://en.wikipedia.org/wiki/Quartic_function |
| 14 |
return ( (x + 4.0) * (x + 1.0) * (x - 1.0) * (x - 3.0) ) / 14.0 + 0.5; |
| 15 |
} |
| 16 |
// Without cache, with array (greedy) |
| 17 |
auto result1 = iota(-4, 5).map!(a =>tuple(a, fun(a)))() |
| 18 |
.filter!(a => a[1] < 0)() |
| 19 |
.map!(a => a[0])() |
| 20 |
.array(); |
| 21 |
|
| 22 |
// the values of x that have a negative y are: |
| 23 |
assert(equal(result1, [-3, -2, 2])); |
| 24 |
|
| 25 |
// Check how many times fun was evaluated. |
| 26 |
// As many times as the number of items in both source and result. |
| 27 |
assert(counter == iota(-4, 5).length + result1.length); |
| 28 |
|
| 29 |
counter = 0; |
| 30 |
// Without array, with cache (lazy) |
| 31 |
auto result2 = iota(-4, 5).map!(a =>tuple(a, fun(a)))() |
| 32 |
.cache() |
| 33 |
.filter!(a => a[1] < 0)() |
| 34 |
.map!(a => a[0])(); |
| 35 |
|
| 36 |
// the values of x that have a negative y are: |
| 37 |
assert(equal(result2, [-3, -2, 2])); |
| 38 |
|
| 39 |
// Check how many times fun was evaluated. |
| 40 |
// Only as many times as the number of items in source. |
| 41 |
assert(counter == iota(-4, 5).length); |
| 42 |
} |
| 43 |
|
| 44 |
@safe unittest |
| 45 |
{ |
| 46 |
import std.algorithm.iteration; |
| 47 |
|
| 48 |
import std.algorithm.comparison : equal; |
| 49 |
import std.range; |
| 50 |
int i = 0; |
| 51 |
|
| 52 |
auto r = iota(0, 4).tee!((a){i = a;}, No.pipeOnPop); |
| 53 |
auto r1 = r.take(3).cache(); |
| 54 |
auto r2 = r.cache().take(3); |
| 55 |
|
| 56 |
assert(equal(r1, [0, 1, 2])); |
| 57 |
assert(i == 2); //The last "seen" element was 2. The data in cache has been cleared. |
| 58 |
|
| 59 |
assert(equal(r2, [0, 1, 2])); |
| 60 |
assert(i == 3); //cache has accessed 3. It is still stored internally by cache. |
| 61 |
} |
| 62 |
|
| 63 |
@safe @nogc unittest |
| 64 |
{ |
| 65 |
import std.algorithm.iteration; |
| 66 |
|
| 67 |
import std.algorithm.comparison : equal; |
| 68 |
import std.range : chain, only; |
| 69 |
auto squares = |
| 70 |
chain(only(1, 2, 3, 4), only(5, 6)).map!(a => a * a); |
| 71 |
assert(equal(squares, only(1, 4, 9, 16, 25, 36))); |
| 72 |
} |
| 73 |
|
| 74 |
@safe unittest |
| 75 |
{ |
| 76 |
import std.algorithm.iteration; |
| 77 |
|
| 78 |
auto sums = [2, 4, 6, 8]; |
| 79 |
auto products = [1, 4, 9, 16]; |
| 80 |
|
| 81 |
size_t i = 0; |
| 82 |
foreach (result; [ 1, 2, 3, 4 ].map!("a + a", "a * a")) |
| 83 |
{ |
| 84 |
assert(result[0] == sums[i]); |
| 85 |
assert(result[1] == products[i]); |
| 86 |
++i; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
@safe unittest |
| 91 |
{ |
| 92 |
import std.algorithm.iteration; |
| 93 |
|
| 94 |
import std.algorithm.comparison : equal; |
| 95 |
import std.conv : to; |
| 96 |
|
| 97 |
alias stringize = map!(to!string); |
| 98 |
assert(equal(stringize([ 1, 2, 3, 4 ]), [ "1", "2", "3", "4" ])); |
| 99 |
} |
| 100 |
|
| 101 |
@safe unittest |
| 102 |
{ |
| 103 |
import std.algorithm.iteration; |
| 104 |
|
| 105 |
import std.range : iota; |
| 106 |
import std.typecons : No; |
| 107 |
|
| 108 |
int[] arr; |
| 109 |
iota(5).each!(n => arr ~= n); |
| 110 |
assert(arr == [0, 1, 2, 3, 4]); |
| 111 |
|
| 112 |
// stop iterating early |
| 113 |
iota(5).each!((n) { arr ~= n; return No.each; }); |
| 114 |
assert(arr == [0, 1, 2, 3, 4, 0]); |
| 115 |
|
| 116 |
// If the range supports it, the value can be mutated in place |
| 117 |
arr.each!((ref n) => n++); |
| 118 |
assert(arr == [1, 2, 3, 4, 5, 1]); |
| 119 |
|
| 120 |
arr.each!"a++"; |
| 121 |
assert(arr == [2, 3, 4, 5, 6, 2]); |
| 122 |
|
| 123 |
auto m = arr.map!(n => n); |
| 124 |
// by-ref lambdas are not allowed for non-ref ranges |
| 125 |
static assert(!__traits(compiles, m.each!((ref n) => n++))); |
| 126 |
|
| 127 |
// The default predicate consumes the range |
| 128 |
(&m).each(); |
| 129 |
assert(m.empty); |
| 130 |
} |
| 131 |
|
| 132 |
@safe unittest |
| 133 |
{ |
| 134 |
import std.algorithm.iteration; |
| 135 |
|
| 136 |
auto arr = new size_t[4]; |
| 137 |
|
| 138 |
arr.each!"a=i"(); |
| 139 |
assert(arr == [0, 1, 2, 3]); |
| 140 |
|
| 141 |
arr.each!((i, ref e) => e = i * 2); |
| 142 |
assert(arr == [0, 2, 4, 6]); |
| 143 |
} |
| 144 |
|
| 145 |
@system unittest |
| 146 |
{ |
| 147 |
import std.algorithm.iteration; |
| 148 |
|
| 149 |
static class S |
| 150 |
{ |
| 151 |
int x; |
| 152 |
int opApply(scope int delegate(ref int _x) dg) { return dg(x); } |
| 153 |
} |
| 154 |
|
| 155 |
auto s = new S; |
| 156 |
s.each!"a++"; |
| 157 |
assert(s.x == 1); |
| 158 |
} |
| 159 |
|
| 160 |
@safe unittest |
| 161 |
{ |
| 162 |
import std.algorithm.iteration; |
| 163 |
|
| 164 |
import std.algorithm.comparison : equal; |
| 165 |
import std.math.operations : isClose; |
| 166 |
import std.range; |
| 167 |
|
| 168 |
int[] arr = [ 1, 2, 3, 4, 5 ]; |
| 169 |
|
| 170 |
// Filter below 3 |
| 171 |
auto small = filter!(a => a < 3)(arr); |
| 172 |
assert(equal(small, [ 1, 2 ])); |
| 173 |
|
| 174 |
// Filter again, but with Uniform Function Call Syntax (UFCS) |
| 175 |
auto sum = arr.filter!(a => a < 3); |
| 176 |
assert(equal(sum, [ 1, 2 ])); |
| 177 |
|
| 178 |
// In combination with chain() to span multiple ranges |
| 179 |
int[] a = [ 3, -2, 400 ]; |
| 180 |
int[] b = [ 100, -101, 102 ]; |
| 181 |
auto r = chain(a, b).filter!(a => a > 0); |
| 182 |
assert(equal(r, [ 3, 400, 100, 102 ])); |
| 183 |
|
| 184 |
// Mixing convertible types is fair game, too |
| 185 |
double[] c = [ 2.5, 3.0 ]; |
| 186 |
auto r1 = chain(c, a, b).filter!(a => cast(int) a != a); |
| 187 |
assert(isClose(r1, [ 2.5 ])); |
| 188 |
} |
| 189 |
|
| 190 |
@safe unittest |
| 191 |
{ |
| 192 |
import std.algorithm.iteration; |
| 193 |
|
| 194 |
import std.algorithm.comparison : equal; |
| 195 |
import std.range; |
| 196 |
|
| 197 |
int[] arr = [ 1, 2, 3, 4, 5 ]; |
| 198 |
auto small = filterBidirectional!("a < 3")(arr); |
| 199 |
static assert(isBidirectionalRange!(typeof(small))); |
| 200 |
assert(small.back == 2); |
| 201 |
assert(equal(small, [ 1, 2 ])); |
| 202 |
assert(equal(retro(small), [ 2, 1 ])); |
| 203 |
// In combination with chain() to span multiple ranges |
| 204 |
int[] a = [ 3, -2, 400 ]; |
| 205 |
int[] b = [ 100, -101, 102 ]; |
| 206 |
auto r = filterBidirectional!("a > 0")(chain(a, b)); |
| 207 |
assert(r.back == 102); |
| 208 |
} |
| 209 |
|
| 210 |
@safe unittest |
| 211 |
{ |
| 212 |
import std.algorithm.iteration; |
| 213 |
|
| 214 |
import std.algorithm.comparison : equal; |
| 215 |
import std.typecons : tuple, Tuple; |
| 216 |
|
| 217 |
int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ]; |
| 218 |
assert(equal(group(arr), [ tuple(1, 1u), tuple(2, 4u), tuple(3, 1u), |
| 219 |
tuple(4, 3u), tuple(5, 1u) ][])); |
| 220 |
} |
| 221 |
|
| 222 |
@safe unittest |
| 223 |
{ |
| 224 |
import std.algorithm.iteration; |
| 225 |
|
| 226 |
import std.algorithm.sorting : sort; |
| 227 |
import std.array : assocArray; |
| 228 |
|
| 229 |
uint[string] result; |
| 230 |
auto range = ["a", "b", "a", "c", "b", "c", "c", "d", "e"]; |
| 231 |
result = range.sort!((a, b) => a < b) |
| 232 |
.group |
| 233 |
.assocArray; |
| 234 |
|
| 235 |
assert(result == ["a": 2U, "b": 2U, "c": 3U, "d": 1U, "e": 1U]); |
| 236 |
} |
| 237 |
|
| 238 |
@safe unittest |
| 239 |
{ |
| 240 |
import std.algorithm.iteration; |
| 241 |
|
| 242 |
import std.algorithm.comparison : equal; |
| 243 |
|
| 244 |
// Grouping by particular attribute of each element: |
| 245 |
auto data = [ |
| 246 |
[1, 1], |
| 247 |
[1, 2], |
| 248 |
[2, 2], |
| 249 |
[2, 3] |
| 250 |
]; |
| 251 |
|
| 252 |
auto r1 = data.chunkBy!((a,b) => a[0] == b[0]); |
| 253 |
assert(r1.equal!equal([ |
| 254 |
[[1, 1], [1, 2]], |
| 255 |
[[2, 2], [2, 3]] |
| 256 |
])); |
| 257 |
|
| 258 |
auto r2 = data.chunkBy!((a,b) => a[1] == b[1]); |
| 259 |
assert(r2.equal!equal([ |
| 260 |
[[1, 1]], |
| 261 |
[[1, 2], [2, 2]], |
| 262 |
[[2, 3]] |
| 263 |
])); |
| 264 |
} |
| 265 |
|
| 266 |
@safe unittest |
| 267 |
{ |
| 268 |
import std.algorithm.iteration; |
| 269 |
|
| 270 |
import std.algorithm.comparison : equal; |
| 271 |
import std.range.primitives; |
| 272 |
import std.typecons : tuple; |
| 273 |
|
| 274 |
// Grouping by particular attribute of each element: |
| 275 |
auto range = |
| 276 |
[ |
| 277 |
[1, 1], |
| 278 |
[1, 1], |
| 279 |
[1, 2], |
| 280 |
[2, 2], |
| 281 |
[2, 3], |
| 282 |
[2, 3], |
| 283 |
[3, 3] |
| 284 |
]; |
| 285 |
|
| 286 |
auto byX = chunkBy!(a => a[0])(range); |
| 287 |
auto expected1 = |
| 288 |
[ |
| 289 |
tuple(1, [[1, 1], [1, 1], [1, 2]]), |
| 290 |
tuple(2, [[2, 2], [2, 3], [2, 3]]), |
| 291 |
tuple(3, [[3, 3]]) |
| 292 |
]; |
| 293 |
foreach (e; byX) |
| 294 |
{ |
| 295 |
assert(!expected1.empty); |
| 296 |
assert(e[0] == expected1.front[0]); |
| 297 |
assert(e[1].equal(expected1.front[1])); |
| 298 |
expected1.popFront(); |
| 299 |
} |
| 300 |
|
| 301 |
auto byY = chunkBy!(a => a[1])(range); |
| 302 |
auto expected2 = |
| 303 |
[ |
| 304 |
tuple(1, [[1, 1], [1, 1]]), |
| 305 |
tuple(2, [[1, 2], [2, 2]]), |
| 306 |
tuple(3, [[2, 3], [2, 3], [3, 3]]) |
| 307 |
]; |
| 308 |
foreach (e; byY) |
| 309 |
{ |
| 310 |
assert(!expected2.empty); |
| 311 |
assert(e[0] == expected2.front[0]); |
| 312 |
assert(e[1].equal(expected2.front[1])); |
| 313 |
expected2.popFront(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
nothrow pure @safe unittest |
| 318 |
{ |
| 319 |
import std.algorithm.iteration; |
| 320 |
|
| 321 |
import std.algorithm.comparison : equal; |
| 322 |
import std.range : dropExactly; |
| 323 |
auto source = [4, 3, 2, 11, 0, -3, -3, 5, 3, 0]; |
| 324 |
|
| 325 |
auto result1 = source.splitWhen!((a,b) => a <= b); |
| 326 |
assert(result1.save.equal!equal([ |
| 327 |
[4, 3, 2], |
| 328 |
[11, 0, -3], |
| 329 |
[-3], |
| 330 |
[5, 3, 0] |
| 331 |
])); |
| 332 |
|
| 333 |
//splitWhen, like chunkBy, is currently a reference range (this may change |
| 334 |
//in future). Remember to call `save` when appropriate. |
| 335 |
auto result2 = result1.dropExactly(2); |
| 336 |
assert(result1.save.equal!equal([ |
| 337 |
[-3], |
| 338 |
[5, 3, 0] |
| 339 |
])); |
| 340 |
} |
| 341 |
|
| 342 |
@safe unittest |
| 343 |
{ |
| 344 |
import std.algorithm.iteration; |
| 345 |
|
| 346 |
import std.algorithm.comparison : equal; |
| 347 |
import std.conv : text; |
| 348 |
|
| 349 |
assert(["abc", "def"].joiner.equal("abcdef")); |
| 350 |
assert(["Mary", "has", "a", "little", "lamb"] |
| 351 |
.joiner("...") |
| 352 |
.equal("Mary...has...a...little...lamb")); |
| 353 |
assert(["", "abc"].joiner("xyz").equal("xyzabc")); |
| 354 |
assert([""].joiner("xyz").equal("")); |
| 355 |
assert(["", ""].joiner("xyz").equal("xyz")); |
| 356 |
} |
| 357 |
|
| 358 |
@safe unittest |
| 359 |
{ |
| 360 |
import std.algorithm.iteration; |
| 361 |
|
| 362 |
import std.algorithm.comparison : equal; |
| 363 |
import std.range : repeat; |
| 364 |
|
| 365 |
assert([""].joiner.equal("")); |
| 366 |
assert(["", ""].joiner.equal("")); |
| 367 |
assert(["", "abc"].joiner.equal("abc")); |
| 368 |
assert(["abc", ""].joiner.equal("abc")); |
| 369 |
assert(["abc", "def"].joiner.equal("abcdef")); |
| 370 |
assert(["Mary", "has", "a", "little", "lamb"].joiner.equal("Maryhasalittlelamb")); |
| 371 |
assert("abc".repeat(3).joiner.equal("abcabcabc")); |
| 372 |
} |
| 373 |
|
| 374 |
@safe unittest |
| 375 |
{ |
| 376 |
import std.algorithm.iteration; |
| 377 |
|
| 378 |
import std.algorithm.comparison : equal; |
| 379 |
auto a = [ [1, 2, 3], [42, 43] ]; |
| 380 |
auto j = joiner(a); |
| 381 |
j.front = 44; |
| 382 |
assert(a == [ [44, 2, 3], [42, 43] ]); |
| 383 |
assert(equal(j, [44, 2, 3, 42, 43])); |
| 384 |
} |
| 385 |
|
| 386 |
@safe pure unittest |
| 387 |
{ |
| 388 |
import std.algorithm.iteration; |
| 389 |
|
| 390 |
import std.algorithm.comparison : equal; |
| 391 |
import std.range : chain, cycle, iota, only, retro, take, zip; |
| 392 |
import std.format : format; |
| 393 |
|
| 394 |
static immutable number = "12345678"; |
| 395 |
static immutable delimiter = ","; |
| 396 |
auto formatted = number.retro |
| 397 |
.zip(3.iota.cycle.take(number.length)) |
| 398 |
.map!(z => chain(z[0].only, z[1] == 2 ? delimiter : null)) |
| 399 |
.joiner |
| 400 |
.retro; |
| 401 |
static immutable expected = "12,345,678"; |
| 402 |
assert(formatted.equal(expected)); |
| 403 |
} |
| 404 |
|
| 405 |
@safe unittest |
| 406 |
{ |
| 407 |
import std.algorithm.iteration; |
| 408 |
|
| 409 |
import std.algorithm.comparison : equal; |
| 410 |
import std.range : retro; |
| 411 |
|
| 412 |
auto a = [[1, 2, 3], [4, 5]]; |
| 413 |
auto j = a.joiner; |
| 414 |
j.back = 44; |
| 415 |
assert(a == [[1, 2, 3], [4, 44]]); |
| 416 |
assert(equal(j.retro, [44, 4, 3, 2, 1])); |
| 417 |
} |
| 418 |
|
| 419 |
@safe unittest |
| 420 |
{ |
| 421 |
import std.algorithm.iteration; |
| 422 |
|
| 423 |
import std.algorithm.comparison : max, min; |
| 424 |
import std.math.operations : isClose; |
| 425 |
import std.range; |
| 426 |
|
| 427 |
int[] arr = [ 1, 2, 3, 4, 5 ]; |
| 428 |
// Sum all elements |
| 429 |
auto sum = reduce!((a,b) => a + b)(0, arr); |
| 430 |
assert(sum == 15); |
| 431 |
|
| 432 |
// Sum again, using a string predicate with "a" and "b" |
| 433 |
sum = reduce!"a + b"(0, arr); |
| 434 |
assert(sum == 15); |
| 435 |
|
| 436 |
// Compute the maximum of all elements |
| 437 |
auto largest = reduce!(max)(arr); |
| 438 |
assert(largest == 5); |
| 439 |
|
| 440 |
// Max again, but with Uniform Function Call Syntax (UFCS) |
| 441 |
largest = arr.reduce!(max); |
| 442 |
assert(largest == 5); |
| 443 |
|
| 444 |
// Compute the number of odd elements |
| 445 |
auto odds = reduce!((a,b) => a + (b & 1))(0, arr); |
| 446 |
assert(odds == 3); |
| 447 |
|
| 448 |
// Compute the sum of squares |
| 449 |
auto ssquares = reduce!((a,b) => a + b * b)(0, arr); |
| 450 |
assert(ssquares == 55); |
| 451 |
|
| 452 |
// Chain multiple ranges into seed |
| 453 |
int[] a = [ 3, 4 ]; |
| 454 |
int[] b = [ 100 ]; |
| 455 |
auto r = reduce!("a + b")(chain(a, b)); |
| 456 |
assert(r == 107); |
| 457 |
|
| 458 |
// Mixing convertible types is fair game, too |
| 459 |
double[] c = [ 2.5, 3.0 ]; |
| 460 |
auto r1 = reduce!("a + b")(chain(a, b, c)); |
| 461 |
assert(isClose(r1, 112.5)); |
| 462 |
|
| 463 |
// To minimize nesting of parentheses, Uniform Function Call Syntax can be used |
| 464 |
auto r2 = chain(a, b, c).reduce!("a + b"); |
| 465 |
assert(isClose(r2, 112.5)); |
| 466 |
} |
| 467 |
|
| 468 |
@safe unittest |
| 469 |
{ |
| 470 |
import std.algorithm.iteration; |
| 471 |
|
| 472 |
import std.algorithm.comparison : max, min; |
| 473 |
import std.math.operations : isClose; |
| 474 |
import std.math.algebraic : sqrt; |
| 475 |
import std.typecons : tuple, Tuple; |
| 476 |
|
| 477 |
double[] a = [ 3.0, 4, 7, 11, 3, 2, 5 ]; |
| 478 |
// Compute minimum and maximum in one pass |
| 479 |
auto r = reduce!(min, max)(a); |
| 480 |
// The type of r is Tuple!(int, int) |
| 481 |
assert(isClose(r[0], 2)); // minimum |
| 482 |
assert(isClose(r[1], 11)); // maximum |
| 483 |
|
| 484 |
// Compute sum and sum of squares in one pass |
| 485 |
r = reduce!("a + b", "a + b * b")(tuple(0.0, 0.0), a); |
| 486 |
assert(isClose(r[0], 35)); // sum |
| 487 |
assert(isClose(r[1], 233)); // sum of squares |
| 488 |
// Compute average and standard deviation from the above |
| 489 |
auto avg = r[0] / a.length; |
| 490 |
assert(avg == 5); |
| 491 |
auto stdev = sqrt(r[1] / a.length - avg * avg); |
| 492 |
assert(cast(int) stdev == 2); |
| 493 |
} |
| 494 |
|
| 495 |
@safe pure unittest |
| 496 |
{ |
| 497 |
import std.algorithm.iteration; |
| 498 |
|
| 499 |
immutable arr = [1, 2, 3, 4, 5]; |
| 500 |
|
| 501 |
// Sum all elements |
| 502 |
assert(arr.fold!((a, e) => a + e) == 15); |
| 503 |
|
| 504 |
// Sum all elements with explicit seed |
| 505 |
assert(arr.fold!((a, e) => a + e)(6) == 21); |
| 506 |
|
| 507 |
import std.algorithm.comparison : min, max; |
| 508 |
import std.typecons : tuple; |
| 509 |
|
| 510 |
// Compute minimum and maximum at the same time |
| 511 |
assert(arr.fold!(min, max) == tuple(1, 5)); |
| 512 |
|
| 513 |
// Compute minimum and maximum at the same time with seeds |
| 514 |
assert(arr.fold!(min, max)(0, 7) == tuple(0, 7)); |
| 515 |
|
| 516 |
// Can be used in a UFCS chain |
| 517 |
assert(arr.map!(a => a + 1).fold!((a, e) => a + e) == 20); |
| 518 |
|
| 519 |
// Return the last element of any range |
| 520 |
assert(arr.fold!((a, e) => e) == 5); |
| 521 |
} |
| 522 |
|
| 523 |
@safe unittest |
| 524 |
{ |
| 525 |
import std.algorithm.iteration; |
| 526 |
|
| 527 |
import std.algorithm.comparison : max, min; |
| 528 |
import std.array : array; |
| 529 |
import std.math.operations : isClose; |
| 530 |
import std.range : chain; |
| 531 |
|
| 532 |
int[] arr = [1, 2, 3, 4, 5]; |
| 533 |
// Partial sum of all elements |
| 534 |
auto sum = cumulativeFold!((a, b) => a + b)(arr, 0); |
| 535 |
assert(sum.array == [1, 3, 6, 10, 15]); |
| 536 |
|
| 537 |
// Partial sum again, using a string predicate with "a" and "b" |
| 538 |
auto sum2 = cumulativeFold!"a + b"(arr, 0); |
| 539 |
assert(sum2.array == [1, 3, 6, 10, 15]); |
| 540 |
|
| 541 |
// Compute the partial maximum of all elements |
| 542 |
auto largest = cumulativeFold!max(arr); |
| 543 |
assert(largest.array == [1, 2, 3, 4, 5]); |
| 544 |
|
| 545 |
// Partial max again, but with Uniform Function Call Syntax (UFCS) |
| 546 |
largest = arr.cumulativeFold!max; |
| 547 |
assert(largest.array == [1, 2, 3, 4, 5]); |
| 548 |
|
| 549 |
// Partial count of odd elements |
| 550 |
auto odds = arr.cumulativeFold!((a, b) => a + (b & 1))(0); |
| 551 |
assert(odds.array == [1, 1, 2, 2, 3]); |
| 552 |
|
| 553 |
// Compute the partial sum of squares |
| 554 |
auto ssquares = arr.cumulativeFold!((a, b) => a + b * b)(0); |
| 555 |
assert(ssquares.array == [1, 5, 14, 30, 55]); |
| 556 |
|
| 557 |
// Chain multiple ranges into seed |
| 558 |
int[] a = [3, 4]; |
| 559 |
int[] b = [100]; |
| 560 |
auto r = cumulativeFold!"a + b"(chain(a, b)); |
| 561 |
assert(r.array == [3, 7, 107]); |
| 562 |
|
| 563 |
// Mixing convertible types is fair game, too |
| 564 |
double[] c = [2.5, 3.0]; |
| 565 |
auto r1 = cumulativeFold!"a + b"(chain(a, b, c)); |
| 566 |
assert(isClose(r1, [3, 7, 107, 109.5, 112.5])); |
| 567 |
|
| 568 |
// To minimize nesting of parentheses, Uniform Function Call Syntax can be used |
| 569 |
auto r2 = chain(a, b, c).cumulativeFold!"a + b"; |
| 570 |
assert(isClose(r2, [3, 7, 107, 109.5, 112.5])); |
| 571 |
} |
| 572 |
|
| 573 |
@safe unittest |
| 574 |
{ |
| 575 |
import std.algorithm.iteration; |
| 576 |
|
| 577 |
import std.algorithm.comparison : max, min; |
| 578 |
import std.algorithm.iteration : map; |
| 579 |
import std.math.operations : isClose; |
| 580 |
import std.typecons : tuple; |
| 581 |
|
| 582 |
double[] a = [3.0, 4, 7, 11, 3, 2, 5]; |
| 583 |
// Compute minimum and maximum in one pass |
| 584 |
auto r = a.cumulativeFold!(min, max); |
| 585 |
// The type of r is Tuple!(int, int) |
| 586 |
assert(isClose(r.map!"a[0]", [3, 3, 3, 3, 3, 2, 2])); // minimum |
| 587 |
assert(isClose(r.map!"a[1]", [3, 4, 7, 11, 11, 11, 11])); // maximum |
| 588 |
|
| 589 |
// Compute sum and sum of squares in one pass |
| 590 |
auto r2 = a.cumulativeFold!("a + b", "a + b * b")(tuple(0.0, 0.0)); |
| 591 |
assert(isClose(r2.map!"a[0]", [3, 7, 14, 25, 28, 30, 35])); // sum |
| 592 |
assert(isClose(r2.map!"a[1]", [9, 25, 74, 195, 204, 208, 233])); // sum of squares |
| 593 |
} |
| 594 |
|
| 595 |
@safe unittest |
| 596 |
{ |
| 597 |
import std.algorithm.iteration; |
| 598 |
|
| 599 |
import std.algorithm.comparison : equal; |
| 600 |
|
| 601 |
assert("a|bc|def".splitter('|').equal([ "a", "bc", "def" ])); |
| 602 |
|
| 603 |
int[] a = [1, 0, 2, 3, 0, 4, 5, 6]; |
| 604 |
int[][] w = [ [1], [2, 3], [4, 5, 6] ]; |
| 605 |
assert(a.splitter(0).equal(w)); |
| 606 |
} |
| 607 |
|
| 608 |
@safe unittest |
| 609 |
{ |
| 610 |
import std.algorithm.iteration; |
| 611 |
|
| 612 |
import std.algorithm.comparison : equal; |
| 613 |
import std.typecons : Yes; |
| 614 |
|
| 615 |
assert("a|bc|def".splitter!("a == b", Yes.keepSeparators)('|') |
| 616 |
.equal([ "a", "|", "bc", "|", "def" ])); |
| 617 |
|
| 618 |
int[] a = [1, 0, 2, 3, 0, 4, 5, 6]; |
| 619 |
int[][] w = [ [1], [0], [2, 3], [0], [4, 5, 6] ]; |
| 620 |
assert(a.splitter!("a == b", Yes.keepSeparators)(0).equal(w)); |
| 621 |
} |
| 622 |
|
| 623 |
@safe unittest |
| 624 |
{ |
| 625 |
import std.algorithm.iteration; |
| 626 |
|
| 627 |
import std.algorithm.comparison : equal; |
| 628 |
|
| 629 |
assert("|ab|".splitter('|').equal([ "", "ab", "" ])); |
| 630 |
assert("ab".splitter('|').equal([ "ab" ])); |
| 631 |
|
| 632 |
assert("a|b||c".splitter('|').equal([ "a", "b", "", "c" ])); |
| 633 |
assert("hello world".splitter(' ').equal([ "hello", "", "world" ])); |
| 634 |
|
| 635 |
auto a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ]; |
| 636 |
auto w = [ [1, 2], [], [3], [4, 5], [] ]; |
| 637 |
assert(a.splitter(0).equal(w)); |
| 638 |
} |
| 639 |
|
| 640 |
@safe unittest |
| 641 |
{ |
| 642 |
import std.algorithm.iteration; |
| 643 |
|
| 644 |
import std.algorithm.comparison : equal; |
| 645 |
import std.typecons : Yes; |
| 646 |
|
| 647 |
assert("|ab|".splitter!("a == b", Yes.keepSeparators)('|') |
| 648 |
.equal([ "", "|", "ab", "|", "" ])); |
| 649 |
assert("ab".splitter!("a == b", Yes.keepSeparators)('|') |
| 650 |
.equal([ "ab" ])); |
| 651 |
|
| 652 |
assert("a|b||c".splitter!("a == b", Yes.keepSeparators)('|') |
| 653 |
.equal([ "a", "|", "b", "|", "", "|", "c" ])); |
| 654 |
assert("hello world".splitter!("a == b", Yes.keepSeparators)(' ') |
| 655 |
.equal([ "hello", " ", "", " ", "world" ])); |
| 656 |
|
| 657 |
auto a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ]; |
| 658 |
auto w = [ [1, 2], [0], [], [0], [3], [0], [4, 5], [0], [] ]; |
| 659 |
assert(a.splitter!("a == b", Yes.keepSeparators)(0).equal(w)); |
| 660 |
} |
| 661 |
|
| 662 |
@safe unittest |
| 663 |
{ |
| 664 |
import std.algorithm.iteration; |
| 665 |
|
| 666 |
import std.algorithm.comparison : equal; |
| 667 |
import std.range : empty; |
| 668 |
|
| 669 |
assert("".splitter('|').empty); |
| 670 |
assert("|".splitter('|').equal([ "", "" ])); |
| 671 |
assert("||".splitter('|').equal([ "", "", "" ])); |
| 672 |
} |
| 673 |
|
| 674 |
@safe unittest |
| 675 |
{ |
| 676 |
import std.algorithm.iteration; |
| 677 |
|
| 678 |
import std.algorithm.comparison : equal; |
| 679 |
import std.typecons : Yes; |
| 680 |
import std.range : empty; |
| 681 |
|
| 682 |
assert("".splitter!("a == b", Yes.keepSeparators)('|').empty); |
| 683 |
assert("|".splitter!("a == b", Yes.keepSeparators)('|') |
| 684 |
.equal([ "", "|", "" ])); |
| 685 |
assert("||".splitter!("a == b", Yes.keepSeparators)('|') |
| 686 |
.equal([ "", "|", "", "|", "" ])); |
| 687 |
} |
| 688 |
|
| 689 |
@safe unittest |
| 690 |
{ |
| 691 |
import std.algorithm.iteration; |
| 692 |
|
| 693 |
import std.algorithm.comparison : equal; |
| 694 |
|
| 695 |
assert("a=>bc=>def".splitter("=>").equal([ "a", "bc", "def" ])); |
| 696 |
assert("a|b||c".splitter("||").equal([ "a|b", "c" ])); |
| 697 |
assert("hello world".splitter(" ").equal([ "hello", "world" ])); |
| 698 |
|
| 699 |
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ]; |
| 700 |
int[][] w = [ [1, 2], [3, 0, 4, 5, 0] ]; |
| 701 |
assert(a.splitter([0, 0]).equal(w)); |
| 702 |
|
| 703 |
a = [ 0, 0 ]; |
| 704 |
assert(a.splitter([0, 0]).equal([ (int[]).init, (int[]).init ])); |
| 705 |
|
| 706 |
a = [ 0, 0, 1 ]; |
| 707 |
assert(a.splitter([0, 0]).equal([ [], [1] ])); |
| 708 |
} |
| 709 |
|
| 710 |
@safe unittest |
| 711 |
{ |
| 712 |
import std.algorithm.iteration; |
| 713 |
|
| 714 |
import std.algorithm.comparison : equal; |
| 715 |
import std.typecons : Yes; |
| 716 |
|
| 717 |
assert("a=>bc=>def".splitter!("a == b", Yes.keepSeparators)("=>") |
| 718 |
.equal([ "a", "=>", "bc", "=>", "def" ])); |
| 719 |
assert("a|b||c".splitter!("a == b", Yes.keepSeparators)("||") |
| 720 |
.equal([ "a|b", "||", "c" ])); |
| 721 |
assert("hello world".splitter!("a == b", Yes.keepSeparators)(" ") |
| 722 |
.equal([ "hello", " ", "world" ])); |
| 723 |
|
| 724 |
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ]; |
| 725 |
int[][] w = [ [1, 2], [0, 0], [3, 0, 4, 5, 0] ]; |
| 726 |
assert(a.splitter!("a == b", Yes.keepSeparators)([0, 0]).equal(w)); |
| 727 |
|
| 728 |
a = [ 0, 0 ]; |
| 729 |
assert(a.splitter!("a == b", Yes.keepSeparators)([0, 0]) |
| 730 |
.equal([ (int[]).init, [0, 0], (int[]).init ])); |
| 731 |
|
| 732 |
a = [ 0, 0, 1 ]; |
| 733 |
assert(a.splitter!("a == b", Yes.keepSeparators)([0, 0]) |
| 734 |
.equal([ [], [0, 0], [1] ])); |
| 735 |
} |
| 736 |
|
| 737 |
@safe unittest |
| 738 |
{ |
| 739 |
import std.algorithm.iteration; |
| 740 |
|
| 741 |
import std.algorithm.comparison : equal; |
| 742 |
import std.ascii : toLower; |
| 743 |
|
| 744 |
assert("abXcdxef".splitter!"a.toLower == b"('x').equal( |
| 745 |
[ "ab", "cd", "ef" ])); |
| 746 |
|
| 747 |
auto w = [ [0], [1], [2] ]; |
| 748 |
assert(w.splitter!"a.front == b"(1).equal([ [[0]], [[2]] ])); |
| 749 |
} |
| 750 |
|
| 751 |
@safe unittest |
| 752 |
{ |
| 753 |
import std.algorithm.iteration; |
| 754 |
|
| 755 |
import std.algorithm.comparison : equal; |
| 756 |
import std.typecons : Yes; |
| 757 |
import std.ascii : toLower; |
| 758 |
|
| 759 |
assert("abXcdxef".splitter!("a.toLower == b", Yes.keepSeparators)('x') |
| 760 |
.equal([ "ab", "X", "cd", "x", "ef" ])); |
| 761 |
|
| 762 |
auto w = [ [0], [1], [2] ]; |
| 763 |
assert(w.splitter!("a.front == b", Yes.keepSeparators)(1) |
| 764 |
.equal([ [[0]], [[1]], [[2]] ])); |
| 765 |
} |
| 766 |
|
| 767 |
@safe unittest |
| 768 |
{ |
| 769 |
import std.algorithm.iteration; |
| 770 |
|
| 771 |
import std.algorithm.comparison : equal; |
| 772 |
import std.range.primitives : front; |
| 773 |
|
| 774 |
assert(equal(splitter!(a => a == '|')("a|bc|def"), [ "a", "bc", "def" ])); |
| 775 |
assert(equal(splitter!(a => a == ' ')("hello world"), [ "hello", "", "world" ])); |
| 776 |
|
| 777 |
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ]; |
| 778 |
int[][] w = [ [1, 2], [], [3], [4, 5], [] ]; |
| 779 |
assert(equal(splitter!(a => a == 0)(a), w)); |
| 780 |
|
| 781 |
a = [ 0 ]; |
| 782 |
assert(equal(splitter!(a => a == 0)(a), [ (int[]).init, (int[]).init ])); |
| 783 |
|
| 784 |
a = [ 0, 1 ]; |
| 785 |
assert(equal(splitter!(a => a == 0)(a), [ [], [1] ])); |
| 786 |
|
| 787 |
w = [ [0], [1], [2] ]; |
| 788 |
assert(equal(splitter!(a => a.front == 1)(w), [ [[0]], [[2]] ])); |
| 789 |
} |
| 790 |
|
| 791 |
@safe unittest |
| 792 |
{ |
| 793 |
import std.algorithm.iteration; |
| 794 |
|
| 795 |
import std.algorithm.comparison : equal; |
| 796 |
|
| 797 |
assert("|ab|".splitter('|').equal([ "", "ab", "" ])); |
| 798 |
assert("ab".splitter('|').equal([ "ab" ])); |
| 799 |
} |
| 800 |
|
| 801 |
@safe unittest |
| 802 |
{ |
| 803 |
import std.algorithm.iteration; |
| 804 |
|
| 805 |
import std.algorithm.comparison : equal; |
| 806 |
import std.typecons : Yes; |
| 807 |
|
| 808 |
assert("|ab|".splitter!("a == b", Yes.keepSeparators)('|') |
| 809 |
.equal([ "", "|", "ab", "|", "" ])); |
| 810 |
assert("ab".splitter!("a == b", Yes.keepSeparators)('|') |
| 811 |
.equal([ "ab" ])); |
| 812 |
} |
| 813 |
|
| 814 |
@safe unittest |
| 815 |
{ |
| 816 |
import std.algorithm.iteration; |
| 817 |
|
| 818 |
import std.algorithm.comparison : equal; |
| 819 |
import std.range : retro; |
| 820 |
assert("a|bc|def".splitter('|').retro.equal([ "def", "bc", "a" ])); |
| 821 |
} |
| 822 |
|
| 823 |
@safe unittest |
| 824 |
{ |
| 825 |
import std.algorithm.iteration; |
| 826 |
|
| 827 |
import std.algorithm.comparison : equal; |
| 828 |
import std.typecons : Yes; |
| 829 |
import std.range : retro; |
| 830 |
assert("a|bc|def".splitter!("a == b", Yes.keepSeparators)('|') |
| 831 |
.retro.equal([ "def", "|", "bc", "|", "a" ])); |
| 832 |
} |
| 833 |
|
| 834 |
@safe unittest |
| 835 |
{ |
| 836 |
import std.algorithm.iteration; |
| 837 |
|
| 838 |
import std.ascii : isWhite; |
| 839 |
import std.algorithm.comparison : equal; |
| 840 |
import std.algorithm.iteration : splitter; |
| 841 |
|
| 842 |
string str = "Hello World!"; |
| 843 |
assert(str.splitter!(isWhite).equal(["Hello", "World!"])); |
| 844 |
} |
| 845 |
|
| 846 |
@safe pure unittest |
| 847 |
{ |
| 848 |
import std.algorithm.iteration; |
| 849 |
|
| 850 |
import std.algorithm.comparison : equal; |
| 851 |
auto a = " a bcd ef gh "; |
| 852 |
assert(equal(splitter(a), ["a", "bcd", "ef", "gh"][])); |
| 853 |
} |
| 854 |
|
| 855 |
@safe pure unittest |
| 856 |
{ |
| 857 |
import std.algorithm.iteration; |
| 858 |
|
| 859 |
import std.algorithm.comparison : equal; |
| 860 |
|
| 861 |
// substitute single elements |
| 862 |
assert("do_it".substitute('_', ' ').equal("do it")); |
| 863 |
|
| 864 |
// substitute multiple, single elements |
| 865 |
assert("do_it".substitute('_', ' ', |
| 866 |
'd', 'g', |
| 867 |
'i', 't', |
| 868 |
't', 'o') |
| 869 |
.equal("go to")); |
| 870 |
|
| 871 |
// substitute subranges |
| 872 |
assert("do_it".substitute("_", " ", |
| 873 |
"do", "done") |
| 874 |
.equal("done it")); |
| 875 |
|
| 876 |
// substitution works for any ElementType |
| 877 |
int[] x = [1, 2, 3]; |
| 878 |
auto y = x.substitute(1, 0.1); |
| 879 |
assert(y.equal([0.1, 2, 3])); |
| 880 |
static assert(is(typeof(y.front) == double)); |
| 881 |
|
| 882 |
import std.range : retro; |
| 883 |
assert([1, 2, 3].substitute(1, 0.1).retro.equal([3, 2, 0.1])); |
| 884 |
} |
| 885 |
|
| 886 |
@safe pure unittest |
| 887 |
{ |
| 888 |
import std.algorithm.iteration; |
| 889 |
|
| 890 |
import std.algorithm.comparison : equal; |
| 891 |
|
| 892 |
// substitute subranges of a range |
| 893 |
assert("apple_tree".substitute!("apple", "banana", |
| 894 |
"tree", "shrub").equal("banana_shrub")); |
| 895 |
|
| 896 |
// substitute subranges of a range |
| 897 |
assert("apple_tree".substitute!('a', 'b', |
| 898 |
't', 'f').equal("bpple_free")); |
| 899 |
|
| 900 |
// substitute values |
| 901 |
assert('a'.substitute!('a', 'b', 't', 'f') == 'b'); |
| 902 |
} |
| 903 |
|
| 904 |
@safe pure unittest |
| 905 |
{ |
| 906 |
import std.algorithm.iteration; |
| 907 |
|
| 908 |
import std.algorithm.comparison : equal; |
| 909 |
import std.range.primitives : ElementType; |
| 910 |
|
| 911 |
int[3] x = [1, 2, 3]; |
| 912 |
auto y = x[].substitute(1, 0.1) |
| 913 |
.substitute(0.1, 0.2); |
| 914 |
static assert(is(typeof(y.front) == double)); |
| 915 |
assert(y.equal([0.2, 2, 3])); |
| 916 |
|
| 917 |
auto z = "42".substitute('2', '3') |
| 918 |
.substitute('3', '1'); |
| 919 |
static assert(is(ElementType!(typeof(z)) == dchar)); |
| 920 |
assert(equal(z, "41")); |
| 921 |
} |
| 922 |
|
| 923 |
@safe pure nothrow unittest |
| 924 |
{ |
| 925 |
import std.algorithm.iteration; |
| 926 |
|
| 927 |
import std.range; |
| 928 |
|
| 929 |
//simple integral sumation |
| 930 |
assert(sum([ 1, 2, 3, 4]) == 10); |
| 931 |
|
| 932 |
//with integral promotion |
| 933 |
assert(sum([false, true, true, false, true]) == 3); |
| 934 |
assert(sum(ubyte.max.repeat(100)) == 25500); |
| 935 |
|
| 936 |
//The result may overflow |
| 937 |
assert(uint.max.repeat(3).sum() == 4294967293U ); |
| 938 |
//But a seed can be used to change the sumation primitive |
| 939 |
assert(uint.max.repeat(3).sum(ulong.init) == 12884901885UL); |
| 940 |
|
| 941 |
//Floating point sumation |
| 942 |
assert(sum([1.0, 2.0, 3.0, 4.0]) == 10); |
| 943 |
|
| 944 |
//Floating point operations have double precision minimum |
| 945 |
static assert(is(typeof(sum([1F, 2F, 3F, 4F])) == double)); |
| 946 |
assert(sum([1F, 2, 3, 4]) == 10); |
| 947 |
|
| 948 |
//Force pair-wise floating point sumation on large integers |
| 949 |
import std.math.operations : isClose; |
| 950 |
assert(iota(ulong.max / 2, ulong.max / 2 + 4096).sum(0.0) |
| 951 |
.isClose((ulong.max / 2) * 4096.0 + 4096^^2 / 2)); |
| 952 |
} |
| 953 |
|
| 954 |
@safe @nogc pure nothrow unittest |
| 955 |
{ |
| 956 |
import std.algorithm.iteration; |
| 957 |
|
| 958 |
import std.math.operations : isClose; |
| 959 |
import std.math.traits : isNaN; |
| 960 |
|
| 961 |
static immutable arr1 = [1, 2, 3]; |
| 962 |
static immutable arr2 = [1.5, 2.5, 12.5]; |
| 963 |
|
| 964 |
assert(arr1.mean.isClose(2)); |
| 965 |
assert(arr2.mean.isClose(5.5)); |
| 966 |
|
| 967 |
assert(arr1[0 .. 0].mean.isNaN); |
| 968 |
} |
| 969 |
|
| 970 |
@safe unittest |
| 971 |
{ |
| 972 |
import std.algorithm.iteration; |
| 973 |
|
| 974 |
import std.algorithm.comparison : equal; |
| 975 |
import std.algorithm.mutation : copy; |
| 976 |
|
| 977 |
int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ]; |
| 978 |
assert(equal(uniq(arr), [ 1, 2, 3, 4, 5 ][])); |
| 979 |
|
| 980 |
// Filter duplicates in-place using copy |
| 981 |
arr.length -= arr.uniq().copy(arr).length; |
| 982 |
assert(arr == [ 1, 2, 3, 4, 5 ]); |
| 983 |
|
| 984 |
// Note that uniqueness is only determined consecutively; duplicated |
| 985 |
// elements separated by an intervening different element will not be |
| 986 |
// eliminated: |
| 987 |
assert(equal(uniq([ 1, 1, 2, 1, 1, 3, 1]), [1, 2, 1, 3, 1])); |
| 988 |
} |
| 989 |
|
| 990 |
@safe unittest |
| 991 |
{ |
| 992 |
import std.algorithm.iteration; |
| 993 |
|
| 994 |
import std.algorithm.comparison : equal; |
| 995 |
import std.range : iota; |
| 996 |
assert(equal!equal(iota(3).permutations, |
| 997 |
[[0, 1, 2], |
| 998 |
[1, 0, 2], |
| 999 |
[2, 0, 1], |
| 1000 |
[0, 2, 1], |
| 1001 |
[1, 2, 0], |
| 1002 |
[2, 1, 0]])); |
| 1003 |
} |
| 1004 |
|