Skip to content

Commit

Permalink
Update the equals function to fall back to structural equality
Browse files Browse the repository at this point in the history
When comparing two values that are:

1. of the same type; and
2. do not provide a 'fantasy-land/equals'-method; but
3. also don't have a known type with a Z-provided implementation

then previously Z.equals would return false, but this false is just as
reliable as a true would be.

After this commit, instead of returning false, the Z.equals now returns
the structural equality of the two inputs.
  • Loading branch information
Avaq committed Apr 24, 2021
1 parent d79feb1 commit 82fb5b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,12 +1178,16 @@

//# equals :: (a, b) -> Boolean
//.
//. Returns `true` if its arguments are of the same type and equal according
//. to the type's [`fantasy-land/equals`][] method; `false` otherwise.
//. Returns `true` if the two arguments given are structurally equal. When
//. comparing two values, equality is determined based on:
//.
//. `fantasy-land/equals` implementations are provided for the following
//. built-in types: Null, Undefined, Boolean, Number, Date, RegExp, String,
//. Array, Arguments, Error, Object, and Function.
//. - The types of the two values. Values of differing types unequal.
//. - The [`fantasy-land/equals`][] method of the first value when present.
//. - For Null, Undefined, Boolean, Number, Date, RegExp, String, Array,
//. Arguments, Error, Object, and Function, Sanctuary Type Classes provides
//. `fantasy-land/equals` implementations.
//. - Any other types are structurally compared by recursively comparing the
//. values at each of the keys.
//.
//. The algorithm supports circular data structures. Two arrays are equal
//. if they have the same index paths and for each path have equal values.
Expand Down Expand Up @@ -1219,9 +1223,9 @@

$pairs.push ([x, y]);
try {
return Setoid.test (x) &&
Setoid.test (y) &&
Setoid.methods.equals (x) (y);
return Setoid.test (x) ?
Setoid.methods.equals (x) (y) :
Object$prototype$equals.call (x, y);
} finally {
$pairs.pop ();
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ test ('equals', function() {
eq (Z.equals (Math.sin, Math.cos), false);
eq (Z.equals (Identity (Identity (Identity (0))), Identity (Identity (Identity (0)))), true);
eq (Z.equals (Identity (Identity (Identity (0))), Identity (Identity (Identity (1)))), false);
eq (Z.equals (Useless, Useless), false);
eq (Z.equals (Useless, Useless), true);
eq (Z.equals (Array.prototype, Array.prototype), true);
eq (Z.equals (Nothing.constructor, Maybe), true);
eq (Z.equals ((Just (0)).constructor, Maybe), true);
Expand Down

0 comments on commit 82fb5b2

Please sign in to comment.