hemanth / functional-programming-jargon

Jargon from the functional programming world in simple terms!
http://git.io/fp-jargons
MIT License
18.59k stars 1.02k forks source link

Setoid example #82

Open chamini2 opened 8 years ago

chamini2 commented 8 years ago

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
jethrolarson commented 8 years ago

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.

chamini2 commented 8 years ago

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.

jethrolarson commented 8 years ago

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 notifications@github.com 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 .

chamini2 commented 7 years ago

Well, this is code that can be copied.