compact

Compacts a range

  1. auto compact(Range range)
    compact
    (
    alias pred = null
    Range
    )
    (
    Range range
    )
    if (
    from!"std.range".isInputRange!Range
    )
  2. auto compact(Values values)

Parameters

pred

a unary predicate that returns true if value should be compacted

range
Type: Range

an input range

Return Value

Type: auto

compacted range

Examples

1 import optional: no, some;
2 import ddash.utils: isFalsey;
3 assert([0, 1, 2, 0, 3].compact!(isFalsey).equal([1, 2, 3]));
4 assert([[1], [], [2]].compact!isFalsey.equal([[1], [2]]));
5 assert([some(2), no!int].compact!isFalsey.equal([some(2)]));
6 
7 class C {
8     int i;
9     this(int i) { this.i = i; }
10 }
11 
12 import std.algorithm: map;
13 auto arr = [new C(1), null, new C(2), null];
14 assert(arr.compact.map!(a => a.i).equal([1, 2]));

Meta

Since

0.0.1