Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setoid example #82

Open
chamini2 opened this issue Jul 26, 2016 · 4 comments
Open

Setoid example #82

chamini2 opened this issue Jul 26, 2016 · 4 comments

Comments

@chamini2
Copy link

chamini2 commented Jul 26, 2016

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
#82 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAB-4A0qmzthe_XzQ4fmk8AH4HgyB7i8ks5qZ029gaJpZM4JVQoz
.

@chamini2
Copy link
Author

Well, this is code that can be copied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants