Module ddash.algorithm

Contains a number of algorithms that operate on sequences. These sequences can be:

  • value sequences:

    assert(1.concat(2, 3, 4).array == [1, 2, 3, 4]);

  • ranges:

    assert(1.concat([2, 3, 4]).array == [1, 2, 3, 4]);

  • a mixture of the above two:

    assert(1.concat([2, 3], 4).array == [1, 2, 3, 4]);

  • associative arrays:

    auto aa = ["a": 1, "b": 0, "c": 2];
    assert(aa.compactValues!(a => a == 0) == ["a": 1, "c": 2]);

    Furthermore, a number of algorithms allow you to:

  • operate on members of types:

    This would be akin to passing in a predicate that extracts a member variable from a type to operate on instead of operating on the whole type. These algorithms usually have a By prefix:

    class C {
        int x;
    }
    auto arr1 = [new C(2), new C(3)];
    auto arr2 = [new C(2), new C(3)];
    assert(arr1.equalBy!"x"(arr2));

  • operate via unary or binary predicates:

    import std.math: ceil;
    assert([2.1, 1.2].difference!ceil([2.3, 3.4]).equal([1.2]));
    assert([2.1, 1.2].difference!((a, b) => ceil(a) == ceil(b))([2.3, 3.4]).equal([1.2]));

  • or both:

    struct A {
        int x;
    }
    auto arr = [A(4), A(8), A(12)];
    assert(arr.pullBy!("x", a => a / 2)(5, 9).array == [A(12)]);
  • Algorithms

    Module Functions Properties Description
    compact compact
    compactBy
    compactValues
    Creates a range or associative array with all null/predicate values removed.
    concat concat Concatenates ranges and values together to a new range
    difference difference
    differenceBy
    Creates a range of values not included in the other given set of values
    equal equal
    equalBy
    Tells you if two things are equal
    fill fill mutates Assigns value to each element of input range.
    flatmap flatMap Maps and flattens a range.
    flatten flatten
    flattenDeep
    Flattens a range by removing nesting levels values
    frompairs fromPairs Returns a newly allocated associative array from a range of key/value tuples
    index indexWhere
    lastIndexWhere
    indexOf
    lastIndexOf
    Returns optional index of an element in a range.
    intersection intersection Creates a range of unique values that are included in the other given set of values
    pull pull
    pullIndices
    pullBy
    Pulls elements out of a range
    remove remove mutates Removed elements from a range by unary predicate
    reverse reverse mutates Reverses a range in place
    sort sortBy
    maybeSort
    maybeSortBy
    Provides various ways for sorting a range
    stringify stringify
    stringifySeparatedBy
    Converts all elements in range into a string separated by separator.
    zip zipEach Zips up ranges together