functional-programming-jargon icon indicating copy to clipboard operation
functional-programming-jargon copied to clipboard

Setoid example

Open chamini2 opened this issue 9 years ago • 4 comments

The example of setoid uses equality between the elements of the array but it assumes there's an equality operator (==) between the elements of the setoid.

I would do the following:

Number.prototype.equals = function(num) {
  return num.valueOf() === this.valueOf();
}

(2).equals(2) // true
(2).equals(3) // false

Array.prototype.equals = function(arr) {
    var len = this.length
    if (len !== arr.length) {
        return false
    }
    for (var i = 0; i < len; i++) {
        if (! this[i].equals(arr[i])) {
            return false
        }
    }
    return true
}


[1,2].equals([1,2]) // true
[1,2].equals([1,2,3]) // false
[1,2].equals([2,3]) // false

chamini2 avatar Jul 26 '16 15:07 chamini2

Yeah. It's too bad you have to keep defining your own equality function for everything in JS. Especially since it's generally considered a bad practice to modify native prototypes.

jethrolarson avatar Jul 27 '16 06:07 jethrolarson

I think that the examples are written in JS to make it easier to understand, but they're not the best practices nor intended to be. They're just to clarify any doubt left in the description of the functional programming term.

chamini2 avatar Jul 27 '16 12:07 chamini2

I think the code we display should be moderately practical. It can be incomplete or have uncovered cases. But people should be able to copy it

On Wed, Jul 27, 2016, 5:23 AM Matteo Ferrando [email protected] wrote:

I think that the examples are written in JS to make it easier to understand, but they're not the best practices nor intended to be. They're just to clarify any doubt left in the description of the functional programming term.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/hemanth/functional-programming-jargon/issues/82#issuecomment-235569110, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB-4A0qmzthe_XzQ4fmk8AH4HgyB7i8ks5qZ029gaJpZM4JVQoz .

jethrolarson avatar Jul 27 '16 15:07 jethrolarson

Well, this is code that can be copied.

chamini2 avatar May 26 '17 23:05 chamini2