ddash.algorithm.compact

Compacts a range

Members

Functions

compact
auto compact(Range range)

Compacts a range

compact
auto compact(Values values)

Compacts a list of values

compactBy
auto compactBy(Range range)

Compacts a range by a publicly visible member variable or property of ElemntType!Range

compactValues
auto compactValues(T[U] aa)

Compacts an associative array by its values

Examples

1 import optional: no, some;
2 import ddash.utils: isFalsey;
3 
4 // compact falsy values
5 assert([0, 1, 2, 0, 3].compact!isFalsey.equal([1, 2, 3]));
6 
7 // compact empty arrays
8 assert([[1], [], [2]].compact!isFalsey.equal([[1], [2]]));
9 
10 // compact optionals
11 assert([some(2), no!int].compact!isFalsey.equal([some(2)]));
12 
13 class C {
14     int i;
15     this(int i) { this.i = i; }
16 }
17 
18 import std.algorithm: map;
19 auto arr = [new C(1), null, new C(2), null];
20 assert(arr.compact.map!"a.i".equal([1, 2]));
21 
22 struct A {
23     int x;
24 }
25 
26 // compact by a object member
27 assert([A(7), A(0)].compactBy!("x", isFalsey).equal([A(7)]));
28 
29 // compact an associative array
30 auto aa = ["a": 1, "b": 0, "c": 2];
31 assert(aa.compactValues!isFalsey == ["a": 1, "c": 2]);

Meta