1 /** 2 Tells you if two things are equal 3 */ 4 module ddash.algorithm.equal; 5 6 /// 7 unittest { 8 // single elements 9 assert(!equal(2, 4)); 10 11 // unary predicate function applied to elements then compared 12 assert( equal!(a => a % 2 == 0)(2, 4)); 13 assert( equal!q{a % 2 == 0}(2, 4)); 14 15 // binary predicate used to compare elements 16 assert(!equal!((a, b) => a == b)(2, 4)); 17 assert( equal!q{a != b}(2, 4)); 18 19 // compare ranges of ranges of different range types but same value types 20 import std.algorithm: map, filter; 21 auto r1 = [1, 2, 3].map!"a"; 22 auto r2 = [1, 2, 3].filter!"true"; 23 assert( equal([r1], [r2])); 24 25 assert( equal([1, 2, 3], [1, 2, 3])); 26 assert( equal([[1], [2], [3]], [[1], [2], [3]])); 27 28 static assert(!__traits(compiles, equal!(a => a)(1, "hi"))); 29 } 30 31 /// 32 unittest { 33 struct S { 34 int x; 35 int y; 36 } 37 38 auto s1 = S(1, 2); 39 auto s2 = S(2, 2); 40 41 assert( equalBy!"y"(s1, s2)); 42 assert(!equalBy!"x"(s1, s2)); 43 44 auto r1 = [s2, s2]; 45 auto r2 = [s1, s1]; 46 47 assert( equalBy!"y"(r1, r2)); 48 assert(!equalBy!"x"(r1, r2)); 49 } 50 51 private import eq = ddash.common.equal; 52 53 /** 54 Compares two things together 55 56 It can be customized with a unary or binary predicate. If a unary predicate is provided then it acts as 57 a transformation that is applied to the elements being compared for equality. If a binary predicate is 58 provided then that binary predicate is given the values and must return true or false. 59 60 The_Params: 61 pred = a nullary, unary, or binary predicate 62 lhs = left hand side of == 63 rhs = right hand side of == 64 65 Returns: 66 True if successful evaluation of predicates or values equal 67 68 Since: 69 0.0.1 70 */ 71 alias equal = eq.equal; 72 73 /** 74 Compares two things together by comparing a common publicly visible field of T and U. 75 76 It can be customized with a unary or binary predicate. If a unary predicate is provided then it acts as 77 a transformation that is applies to the elements being compare for equality. If a binary predicate is 78 provided then that binary predicate is given the values and must return true or false. 79 80 The_Params: 81 member = which member in T and U to perform equlity on 82 pred = a nullary, unary, or binary predicate 83 lhs = left hand side of == 84 rhs = right hand side of == 85 86 Returns: 87 True if successful evaluation of predicates or values equal 88 89 Since: 90 0.0.1 91 */ 92 alias equalBy = eq.equalBy;