Module ddash.algorithm.compact

Compacts a range

Example

import ddash.utils: isFalsey, no, some;

// compact falsy values
assert([0, 1, 2, 0, 3].compact!isFalsey.equal([1, 2, 3]));

// compact empty arrays
assert([[1], [], [2]].compact!isFalsey.equal([[1], [2]]));

// compact optionals
assert([some(2), no!int].compact!isFalsey.equal([some(2)]));

class C {
    int i;
    this(int i) { this.i = i; }
}

import std.algorithm: map;
auto arr = [new C(1), null, new C(2), null];
assert(arr.compact.map!"a.i".equal([1, 2]));

struct A {
    int x;
}

// compact by a object member
assert([A(7), A(0)].compactBy!("x", isFalsey).equal([A(7)]));

// compact an associative array
auto aa = ["a": 1, "b": 0, "c": 2];
assert(aa.compactValues!isFalsey == ["a": 1, "c": 2]);

Functions

NameDescription
compact(range) Compacts a range
compact(values) Compacts a list of values
compactBy(range) Compacts a range by a publicly visible member variable or property of ElemntType!Range
compactValues(aa) Compacts an associative array by its values